Data with names through a loop in vector?

조회 수: 2 (최근 30일)
Boris
Boris 2012년 11월 2일
Hello
I try to explain my problem with a simple code. Assuming I have dataset given as below:
z0=0; z1=500; z2=233; z3=677; z4=88; z5=8888; z6=22;
For further calculations, I would like to have all single data in one vector. I can easy write it like this:
z=[z1,z2,z3,z4,z5,z6];
For 6 numbers is this not a problem, but what if I got a set of 1000 such data?
Therefore, I would like to have a loop which creates such a vector automatically. Does anybody have an idea?
Tanks for your hints.
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 2일
where your 1000 variables are saved?
Boris
Boris 2012년 11월 2일
With this 1000, I just wanted specify that It would be also very annoying if I have more then 6 values.
My idea is something like that:
z=[];
for i=1:6;
z=[z,z['num2st(i)]]
end
Unfortunately, the whole problem looks very easy, but I could nowhere find a solution.
Thanks for help

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 2일
If your data are saved in a mat file
data=load('yourfile')
for k=1:1000
out(k)=data.(sprintf('z%d',k))
end
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 2일
Boris, try the above code
Boris
Boris 2012년 11월 2일
Great. Learned a lot. Thanks.

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

추가 답변 (1개)

Matt Fig
Matt Fig 2012년 11월 2일
Did you create this data? It is best to start over and not create such a mess in the first place..... If possible, I would go back and get the data into a proper form, such as a cell array or structure. Then further manipulations will be easier.
Other than that, please read this FAQ, and DON'T DO IT!
  댓글 수: 3
Matt Fig
Matt Fig 2012년 11월 2일
That's o.k. What form is the data in? Is it in a .mat file? If so, see Azzi's suggestion. If not, tell us how the data is stored and we will make further suggestions.
Boris
Boris 2012년 11월 2일
I somehow understand your ideas. The thing I want to have is but somehow different.
Okey, stupidly, I write this in the editor:
z1=200
z2=330
z3=199
But I would like to have it like:
z=[200 330 199]
The only thing I can do, is just write every z value in the vector:
z=[z1 z2 z3]
But, as I wrote to Azzi, I would like to do in loop, something like:
z=[];
for i=1:3;
z=[z,z['num2st(i)]]
end
If I have a .mat file or .txt file, I am able to create an vector/matrix loop wise. But with this simple example, it just did not work.
Hopefully, I pointed out my problem more clear.

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

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by