a proble with saving data with index
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Azzi Abdelmalek
on 4 Nov 2014
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
More Answers (1)
Thomas Pfau
on 4 Nov 2014
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 Comments
Thomas Pfau
on 4 Nov 2014
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
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!