How can i plot a structure array?

Hello all,
I have a structure array containing multiple trials. I would like to plot the singl trials, but change the trials with the arrows (so that I don't have to give the single command). I wrote this code:
for b= 1:length(fields_trials)
plot (Trials.(['a_' int2str(a)]));
end;

3 commentaires

Azzi Abdelmalek
Azzi Abdelmalek le 8 Avr 2016
You wrote the code, and?
Fabio Castro
Fabio Castro le 8 Avr 2016
Sorry, I forgot to write what happened. It plotted only the first trial. My structure is like Trials.a_1, Trials.a_2, ect.
Stephen23
Stephen23 le 8 Avr 2016
Modifié(e) : Stephen23 le 8 Avr 2016
@Fabio Castro: rather than using dynamic fieldnames, you should consider using a non-scalar structure. Using a non-scalar structure will make your code much simpler:
S(1).trial = [1,2,3];
S(2).trial = [4,5,6];
S(3).trial = [7,8,9];
Then accessing the contents of the structure is really simple:
>> S(1).trial % one element
ans =
1 2 3
>> vertcat(S.trial)
ans =
1 2 3
4 5 6
7 8 9
And you can use indexing to access any elements of the array:
>> vertcat(S([false,true,true]).trial)
ans =
4 5 6
7 8 9
Using a non-scalar array would make your code simpler, faster, and less buggy. You shoudl try it.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 8 Avr 2016
Modifié(e) : Walter Roberson le 8 Avr 2016

0 votes

for b= 1:length(fields_trials)
plot (Trials.(['a_' int2str(a)]));
hold on
end

6 commentaires

Walter Roberson
Walter Roberson le 8 Avr 2016
Modifié(e) : Walter Roberson le 8 Avr 2016
If all of the fields are vectors of the same number of data points, and you want to plot all of the fields in the structure, then you can be more efficient.
cols = cellfun(@(C) C(:), struct2cell(Trials), 'Uniform', 0);
y = horzcat(cols{:});
plot(y)
Fabio Castro
Fabio Castro le 8 Avr 2016
Hello, and thank you for your answer. I have just tried your code, but it doesn't work. all I have is just the first one. I was thinking of using the pause command?
Is it possible that the range of values is fairly different between the fields? If so then,
for b= 1:length(fields_trials)
plot (Trials.(['a_' int2str(b)]));
hold on
end
set(gca, 'xlimmode', 'auto', 'ylimmode', 'auto')
Note, by the way, the correction of int2str(a) to int2str(b). I would suggest coding
for b = 1 : length(fields_trials)
plot( Trials.(sprintf('a_%d',b)) );
hold on
end
set(gca, 'xlimmode', 'auto', 'ylimmode', 'auto')
And if you really wanted to get fancy:
hold on
structfun(@plot, Trials, 'Uniform', 0);
set(gca, 'xlimmode', 'auto', 'ylimmode', 'auto')
Fabio Castro
Fabio Castro le 8 Avr 2016
Ok, It works now. However, I have a plot with all the trials overlapping. On the other hand I need to be able to see the single trials, as I need to visually analyze them singularly. Any Idea?
Walter Roberson
Walter Roberson le 8 Avr 2016
Is there to be one subplot per field? Or do you want something like a waterfall plot?
Fabio Castro
Fabio Castro le 8 Avr 2016
Hello Walter,
Yes, I need to be able to plot a trial each time, and be able to go through one by one using the keyboard. This because I need to see if there is any error in the trial. The code you have sent me plots all the trials at the same time.
I hope I have express myself better.
Thank you very much for your help!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by