Different commands for strutures and "regular" matrices
조회 수: 3 (최근 30일)
이전 댓글 표시
I have problems using structures in some commands. I am not able to do the same things with structures as with regular matrices.
Example: I have a "regular" 11x8x6 matrix called gjG. When I write "gjG(11,:,:)", Matlab returns row 11 for the 6 different 11x8 matrices. I have constructed a 1x6 structure gjG that contains the same 6 11x8 matrices as above. When i write "gjG(:).Data(11,:)", Matlab gives the following message: "Scalar index required for this type of multi-level indexing." Does anybody know how I can get around this? This prevents me from making figures in a loop I have written that works only when I use "regular" matrices.
I think the probelm lays in the ":" after "gjG". If I writhe a number between 1 and 6, it works. Also if i only write gjG(:).Data it works. The problem is that in my figure loop I need something that works as if "gjG(:).Data(11,:)" works.
댓글 수: 1
답변 (3개)
Matt J
2013년 6월 3일
편집: Matt J
2013년 6월 3일
Since you're looping, I don't quite understand why you don't loop over gjG(i).Data(11,:) letting i=1...6 individually in the loop.
Failing that, you've chosen an awkward data organization for the kind of manipulations you want to do. The only thing you can really do is convert back to numeric array form,
tmp = cat(3,gjG(:).Data)
tmp(11,:,:),
댓글 수: 2
Matt J
2013년 6월 3일
편집: Matt J
2013년 6월 3일
The second thing you suggested worked, but I want to avoid construting new matrices.
I doubt it's worth it. Any data small enough to plot is small enough to copy. The only other way to do the new things you want is using a nested for-loop:
pline=zeros(1,6);
hold on
for j = 1:8
for i=1:6
pline(i)=gjG(i).Data(11,j);
end
plot(pline, 'color', myColorMap((iAar), :));
set(gca, 'XTickLabel',Alder)
legend((Aar),'location','NE','FontSize',10);
end
hold off
What is the motivation for holding this as a struct ARRAY? What are the other struct fields and their dimensions? If they're all the same, you could combine your data into the fields of a scalar struct. and do things like
for j=1:8
plot(gjG.Data(11,i,:), 'color', myColorMap((iAar), :));
end
for i=1:6
plot(gjG.Data(11,:,i), 'color', myColorMap((iAar), :));
end
Daniel Shub
2013년 6월 3일
Maybe a function like
myfun = @(x, ii)subsref(reshape([x(:).Data], size(x(1).Data, 1), size(x(1).Data, 2), length(x)), struct('type', '()', 'subs', {{ii, ':', ':'}}))
This anonymous function is not very general and if you deviate from the expected inputs bad things will happen ...
To see how it works, first create some dummy data
[gjG(1:6).Data] = deal(randn(11, 8), randn(11, 8), randn(11, 8), randn(11, 8), randn(11, 8), randn(11, 8));
Then call it
myfun(gjG, 11)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!