Possibility to access/use whole structures

Hi!
I made a structure with;
month(1)
month(2)
month(3) etc.
For example, I want to plot month(1:12).Q how can I do this?

Réponses (1)

Guillaume
Guillaume le 22 Fév 2016
See Access multiple elements of a nonscalar struct array. In your case, assuming that the fields Q are scalar:
plot([month(1:12).Q])

3 commentaires

loes7815
loes7815 le 22 Fév 2016
Modifié(e) : loes7815 le 22 Fév 2016
Matlab returns 'vectors must be the same length', so I guess because januari is 31 days and februari 28, thats the problem. How can i solve this? But I also want to plot all Q against another variable. for example
plot([month(1:12).Q],[month(1:12).Z])
and this doesnt work either.
The link doesnt work? I have to enter my licensenumber, but this does not give me access to the page.
Stephen23
Stephen23 le 22 Fév 2016
Modifié(e) : Stephen23 le 22 Fév 2016
Try this link, it does not need a license:
Accessing the elements of a non-scalar structure is easy. Merging unequal-length vectors into one array is not so easy, and essentially the best solution is to preallocate an array and use a loop.
Guillaume
Guillaume le 22 Fév 2016
Modifié(e) : Guillaume le 22 Fév 2016
It wasn't clear from your question whether Q was scalar (in which case you'd want a single plot for all the months) or a vector (in which case you'd want a plot per month).
In the latter case, the easiest is probably:
figure;
hold on;
arrayfun(@(m) plot(m.Q, m.Z), month);
Another option is to stuff the Q and Z in a single cell array and use comma separated list expansion to input arguments:
figure;
QZ = {month.Q; month.Z};
plot(QZ{:}); %Q and Z of each month must be the same size (or scalar)

Cette question est clôturée.

Question posée :

le 22 Fév 2016

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by