Академический Документы
Профессиональный Документы
Культура Документы
Tópicos Avançados
Sumário
1. Estruturas
2. Células
1
Tipos de dados avançados em Matlab
Estruturas
2
Construção de estruturas
Construção de estruturas
Estruturas com repmat
tempo = repmat(struct(’temp’,72,’rainfall’,0.0),1,3);
Todas as estruturas na matriz tempo são inicializadas com
um campo de valores.
Estruturas com matrizes de células (cell array)
tempo = struct(’temp’,{68,80,72}, ’rainfall’,{0.2,0.4,0.0});
As estruturas na matriz tempo são inicializadas com valores
distintos, especificados pelas matrizes das células.
3
Exemplo
MeusPacientes = patient(1:2)
String = patient(2).name
Teste2b = patient(3).test(2,2)
4
Acesso a dados em estruturas
5
Exemplo
A(1).nome='Antonio';
A(1).nasc=1975;
A(1).altura=1.76;
A(1).notas=[12 17 14];
A(2).nome='Manuel';
A(2).nasc=1978;
A(2).altura=1.83;
A(2).notas=[13 16 12];
A
1x2 struct array with B = A(1)
fields: nome: 'Antonio'
nome nasc: 1975
nasc altura: 1.7600
altura notas: [12 17 14]
notas
Exemplo (continuação)
% Acesso a dados utilizando campos dinamicos
vdin='nasc';
A(1).(vdin)=1979;
A(1) =
nome: 'Antonio'
nasc: 1979
altura: 1.7600
notas: [12 17 14]
%Finding the size of Structure Arrays
size(A)
size(A(1).nome)
ans =
1 7
6
Apagar campos da estrutura
% Deleting Fields from Structures
A(2).erro=10;
A
A = rmfield(A, 'erro')
A = A =
1x2 struct array with fields: 1x2 struct array with fields:
nome nome
nasc nasc
altura altura
notas notas
erro
meia_notas_de1 =
%... notar o que representa: 14.3333
[A.nasc]
[A.notas]
ans =
1979 1978
ans =
12 17 14 13 16 12
7
Funções com estrututras
if isa(B,'struct')==0
disp('Nao se encontra esse nome')
end
Exemplo de utilização
>> B=c2x01f(A)
Insira o nome que procura: Antonio
B =
nome: 'Antonio'
nasc: 1979
altura: 1.7600
notas: [12 17 14]
>> B=c2x01f(A)
Insira o nome que procura: xxx
Nao se encontra esse nome
B =
0
8
Organização de dados em estruturas
Exemplo:
Uma imagem a cores pode ser codificada:
Como uma estrutura 1x1 com três campos (imagens de
128x128 correspondentes às componentes R,G,B)
Uma matriz (128x128) de estruturas com três campos cada
(os escalares R,G,B)
Estruturas encadeadas
Pode ser efectuada com a instrução struct:
A = struct(’data’,[3 4 7; 8 0 1],’nest’,...
struct(’testnum’,’Test 1’, ’xdata’,[4 2 8], ’ydata’,[7 1 6]))
9
Exemplo
10
Criação de cell arrays
Cell indexing
A(1,1)={[1 4 3;0 5 8;7 2 9]}; Content indexing
A(1,2)={’Anne Smith’}; A{1,1} = [1 4 3; 0 5 8; 7 2 9];
A(2,1)={3+7i}; A{1,2} = ’Anne Smith’;
A(2,2)={–pi:pi/10:pi} A{2,1} = 3+7i;
A{2,2} = –pi:pi/10:pi;
size(A) = [1 3]
A{1}=123; A{2}=[12.56 1.4]; A{2}(2)=1.4
A{3}=TAI
>> c={1 2 3}
c =
[1] [2] [3]
>> c={1, 2, 3}
c =
[1] [2] [3]
>> size(c(1))
ans =
1 1
11
Mais exemplos
>> size(c{1})
ans =
1 1
>> d=c{1}+2
d =
>> d=c(1)+2
??? Error using ==> +
Function '+' is not defined for values of class 'cell'.
C
B=[{123}, {12.56} ,{‘TAI'}]
12
Exemplo
D =
[] [] [] [] []
[2] 'AI' [12.3000] 'TAI' [144]
Pré-alocação de células
13
Manipulação de células
14
Células como listas de variáveis
15
Células e estruturas
Exemplo
% Inicializaçao dum cell array
A{1,1} = [1 4 3; 0 5 8; 7 2 9];
A{1,2} = 'Anne Smith';
A{2,1} = 3+7i;
A{2,2} = -pi:pi/10:pi;
A
celldisp(A)
cellplot(A)
A =
[3x3 double] 'Anne Smith'
[3.0000+ 7.0000i] [1x21 double]
16
Exemplo
A{1,1} = [1 4 3; 0 5 8; 7 2 9];
A{1,2} = 'Anne Smith';
A{2,1} = 3+7i;
A{1,1} = A{2,2} = -pi:pi/10:pi;
1 4 3
0 5 8
7 2 9
A{2,1} =
3.0000 + 7.0000i
A{1,2} =
Anne Smith
A{2,2} =
Columns 1 through 7
-3.1416 -2.8274 -2.5133 -2.1991 -1.8850 -1.5708 -1.2566
Columns 8 through 14
-0.9425 -0.6283 -0.3142 0 0.3142 0.6283 0.9425
Columns 15 through 21
1.2566 1.5708 1.8850 2.1991 2.5133 2.8274 3.1416
nome=A{1,2}
a11=A{1,1}(2,2)
Elem1=A{2,2}(1)
LastElement=A{2,2}(length(A{2,2}))
nome =
Anne Smith
a11 =
5
Elem1 =
-3.1416
LastElement =
3.1416
17
Acesso subconjuntos e apagar células
% Acesso a subconjuntos
B=A(1,1:2)
B =
[3x3 double] 'Anne Smith'
% Apagar células
A{2,1}=[]
A =
[3x3 double] 'Anne Smith'
[] [1x21 double]
S=cell(1,2);
function [a,b]=c2x02f(c,d,e)
b=1; c=2; d=3;
a=2*c;
I={b,c,d};
b=d+e;
[S{1:2}]=c2x02f(I{1:3});
[j,k]=deal(S{1:2})
j = 2
k = 5
18