How do I use multiple similar structures in a FOR loop?

I've been looking / studying web based documentation / artciles / blogs for 1 week but couldn't find a solution to my problem :[
I would appreciate if someone could help me with this issue.
Example of the problem:
my 1st structure (all is m1 x 1)
dmin1.Date
dmin1.Open
dmin1.High
dmin1.Low
dmin1.Close
my 2nd structure (all is m2 x 1)
dmin2.Date
etc. ...
where m1 ~= m2
the number of structures depends on some inputs, but let's assume there are just these two
What I need to do is call the appropriate structure in a for loop.
For simplicity let's assume I want to find out the length of each structure:
for i = 1 : number of structures (in my example 2)
dates_length(i) = length(dmin"i".Date); % **
% -----------------------
% some code for execution
% -----------------------
end
** for the statement //dmin"i".Date// I need your help - don't know how to correctly write it so matlab will do what I want.
So after the for cycle ends, when I type dates_length in the command window the result should be:
dates_length =
m1 m2
Hope I've explained my issue clearly enough. I'll appreciate any feedback which helps to solve this issue, also the one that might opose the technique I use for sampling the data ... (I've also read the FAQ that such a creation of variables / structures is not recommended, but I can't figure out any other way how to sample the data)

1 Kommentar

Todd Flanagan
Todd Flanagan am 20 Jan. 2011
Michal, I edited your question for clarity. Feel free to update it if you don't think I captured your real question.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 20 Jan. 2011

2 Stimmen

Now, using structures is good -- just use structure arrays.
dmin(1).Date
dmin(1).Open
dmin(2).Date
dmin(2).Open
and so on. Then you would just be using length(dmin(i).Date) which you could even do without a loop by using
arrayfun(@(S) length(S.Date), dmin)
Please also see the FAQ about working with multiple variables

1 Kommentar

Michal
Michal am 20 Jan. 2011
Thank you very much Walter, you've solved my problem !
structure arrays - a very elegant solution.
once again thanks a lot.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by