Different commands for strutures and "regular" matrices

조회 수: 3 (최근 30일)
Karl
Karl 2013년 6월 3일
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.

답변 (3개)

Matt J
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
Karl
Karl 2013년 6월 3일
The second thing you suggested worked, but I want to avoid construting new matrices.
The first thing also works when I plot a graph, but I want to switch axis. Now I get 6 different lines and 8 points on the x-axis. Remember that the structure is 6 11x8 matrices. Instead I want 8 lines with 6 points on the x-axsis. How do I do that?
My loop looks like this now:
Alder = {'<20', '2029','3039','4049','5059','6069','>70', 'all'};
Aar = {'2011', '2012', '2013_1', '2014_1', '2014_s2', '2014_s5'};
nAar = length(Aar);
numberOfColors = nAar;
myColorMap = lines(numberOfColors);
figure
hold on
for iAar = 1:nAar
plot(gjG(iAar).Data(11,:), 'color', myColorMap((iAar), :));
set(gca, 'XTickLabel',Alder)
legend((Aar),'location','NE','FontSize',10);
end
hold off
Matt J
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
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)

Andrei Bobrov
Andrei Bobrov 2013년 6월 4일
out = arrayfun(@(x)gjG(x).Data(11,:),(1:numel(gjG))','un',0);

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by