Different commands for strutures and "regular" matrices

1 view (last 30 days)
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.

Answers (3)

Matt J
Matt J on 3 Jun 2013
Edited: Matt J on 3 Jun 2013
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)
Matt J
Matt J on 3 Jun 2013
Edited: Matt J on 3 Jun 2013
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:
hold on
for j = 1:8
for i=1:6
plot(pline, 'color', myColorMap((iAar), :));
set(gca, 'XTickLabel',Alder)
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), :));
for i=1:6
plot(gjG.Data(11,:,i), 'color', myColorMap((iAar), :));

Sign in to comment.

Daniel Shub
Daniel Shub on 3 Jun 2013
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 on 4 Jun 2013
out = arrayfun(@(x)gjG(x).Data(11,:),(1:numel(gjG))','un',0);


Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!