a proble with saving data with index
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi all,
i have this Problem
i want to save this data in this variable. I did it like this :
%
data_to_be_saved=[1 2 3 4; 5 6 7]
sweepnr=k % k will take 1 to 100
['height_' num2str(sweepnr)]= data_to_ be_saved
['height_' nr2str(k)] doesnt work ,why?
thanks
댓글 수: 0
채택된 답변
Azzi Abdelmalek
2014년 11월 4일
You can do it this way
data_to_be_saved=[1 2 4; 5 6 7]
sweepnr=1
str=['height_' num2str(sweepnr) '= data_to_be_saved']
eval(str)
But you have to read this to see why it's not a good way http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
추가 답변 (1개)
Thomas Pfau
2014년 11월 4일
Hi, What exactly are you trying to do? Are you trying to assign values to the variables height_1 ; height_2 etc ? If so, I doubt, that you can do this in the way you attempt it. In particular I'm not sure, what exactly you want to store in the variables, since your data_to_be_saved matrix is invalid in Matlab (due to the inconsistent dimensions 1x4 and 1x3). I guess what you want to do is assign a value to all entries in the height vector. Which would be achieved by:
height(sweepnr) =
this however works only if there is only a single value in data_to_be_saved.
To store matrices or vectors you will need to assign it individually to each row/slice (and I guess using a for loop). Or you could use a cell array like this:
height = {};
height(k) = {data_to_be_saved}
but as I said, I'm not quite sure what exactly you try to achieve.
댓글 수: 2
Thomas Pfau
2014년 11월 4일
Then this is a classical for loop:
for sweepnr=1:100
% the code gegenrating data_to_be_saved comes here...
eval(['height_' num2str(sweepnr) ' = data_to_be_saved'])
end
And in this instance, I would really suggest using a cell array to store the values, as otherwise you are always using eval. e.g.:
for sweepnr=1:100
% the code gegenrating data_to_be_saved comes here...
height(num2str(sweepnr)) = {data_to_be_saved}
end
in this instance you can simply use height{1} to obtain the data at "height_1" or height{50} for the data at position 50 (and you can easily loop over the data), while in the other instance, you would have to use eval again if you want to access the data in a loop
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!