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

采纳的回答

Azzi Abdelmalek
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)
  1 个评论
Rica
Rica 2014-11-4
Thanks for your answer. I dont want to save the Name, but i want to save the Matrix named data_to_be_saved. this Matrix changes with each sweep.
regards

请先登录,再进行评论。

更多回答(1 个)

Thomas Pfau
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
Rica 2014-11-4
Thanks, Sorry i ask sometimes in a complicates manner. My Goal is easy:
when sweepnr=1 iwant that the data_to_be_saved should be assigned to: height_1.
when sweepnr=100 the data_to_be_saved should be assigned to: height_100. that is all.
Thanks again
Thomas Pfau
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 CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by