How to subplot a structure array in a single window
4 views (last 30 days)
Show older comments
Hello,
I as result of an analysis of some trials, I have a structure array containing all the trials. I want to subplot all the trials in a single window, so that I can have all the trials at one. I wrote this rather inefficient and time consuming code:
subplot(6,6,1),plot(Trials.a_2), title('T2')
subplot(6,6,2),plot(Trials.a_3), title('T3')
subplot(6,6,3),plot(Trials.a_4), title('T4')
subplot(6,6,4),plot(Trials.a_5), title('T5')
subplot(6,6,5),plot(Trials.a_6), title('T6')
subplot(6,6,6),plot(Trials.a_7), title('T7')
subplot(6,6,7),plot(Trials.a_8), title('T8')
subplot(6,6,8),plot(Trials.a_9), title('T9')
subplot(6,6,9),plot(Trials.a_10), title('T10')
subplot(6,6,10),plot(Trials.a_11), title('T11')
subplot(6,6,11),plot(Trials.a_12), title('T12')
subplot(6,6,12),plot(Trials.a_13), title('T13')
subplot(6,6,13),plot(Trials.a_14), title('T14')
subplot(6,6,14),plot(Trials.a_15), title('T15')
subplot(6,6,15),plot(Trials.a_16), title('T16')
subplot(6,6,16),plot(Trials.a_17), title('T17')
subplot(6,6,17),plot(Trials.a_18), title('T18')
subplot(6,6,18),plot(Trials.a_19), title('T19')
subplot(6,6,19),plot(Trials.a_20), title('T20')
subplot(6,6,20),plot(Trials.a_21), title('T21')
subplot(6,6,21),plot(Trials.a_22), title('T22')
subplot(6,6,22),plot(Trials.a_23), title('T23')
subplot(6,6,23),plot(Trials.a_24), title('T24')
subplot(6,6,24),plot(Trials.a_25), title('T25')
subplot(6,6,25),plot(Trials.a_26), title('T26')
subplot(6,6,26),plot(Trials.a_27), title('T27')
subplot(6,6,27),plot(Trials.a_28), title('T28')
subplot(6,6,28),plot(Trials.a_29), title('T29')
subplot(6,6,29),plot(Trials.a_30), title('T30')
subplot(6,6,30),plot(Trials.a_31), title('T31')
subplot(6,6,31),plot(Trials.a_32), title('T32')
subplot(6,6,32),plot(Trials.a_33), title('T33')
subplot(6,6,33),plot(Trials.a_34), title('T34')
The problem I am having is that for different subjects, different numbers of trials and different names of the variable "Trials.a_n", so I have to change manually the names of both the title and the variable "Trails.a_n". I have a cell array called "fields_trials" containing all the names of the trials.
I have tried to create a for loop, but it doesn't work:
for i =1:length(fields_trials{a})
subplot(6,6,i)
plot(MeanDiff.(fields_trials{a})(:,i)), Title (fields_trials{a})
end
What am I doing wrong?
Thank you very much in advance
1 Comment
Jan
on 20 Jan 2017
"It doesn't work" is a weak description of the problem. The readers have to guess, what happens. Please avoid this expression, because it might readers drive crazy, if they want to help, but the problem is kept as a secret. :-)
Answers (1)
Jan
on 20 Jan 2017
Edited: Jan
on 20 Jan 2017
Don't do this.
Do not hide an index in the field names. Using an index as index is much smarter and you will not get such troubles. If the fields are written as an array, the loop is trivial: Trials.a{2} instead of Trials.a_2. Can you change the code, which creates the struct accordingly? If it is not possible, e.g. if it is not your code, ask the author to do so. And if it is really really not possible to change the code, use this:
Fields = fieldnames(Trials);
for i = 1:length(Fields)
subplot(6,6,i);
plot(Trials.(Fields{i})));
Str = strrep(Fields{i}, 'a_', 'T');
title(Str);
end
PS. The code you've posted is confusing
- Where does "MeanDiff" come from suddenly?!
- What is "a" in fields_trials{a})(:,i) and what is the purpose of (:,i) here?
- Title () is not the same as title(), because the case matters in Matlab.
- Why is 'T2' now replaced by the field name?
1 Comment
Stephen23
on 20 Jan 2017
Trials(1).a = ...
Trials(2).a = ...
etc
And if a would be the only field then replacing the whole structure with a cell array or an ND numeric array would likely be the best solution.
See Also
Categories
Find more on Phased Array Design and Analysis 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!