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

Michal, I edited your question for clarity. Feel free to update it if you don't think I captured your real question.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2011년 1월 20일
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

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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by