a proble with saving data with index
显示 更早的评论
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
采纳的回答
更多回答(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 个评论
Rica
2014-11-4
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
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!