a proble with saving data with index

3 views (last 30 days)
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?

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 4 Nov 2014
You can do it this way
data_to_be_saved=[1 2 4; 5 6 7]
str=['height_' num2str(sweepnr) '= data_to_be_saved']
  1 Comment
Rica on 4 Nov 2014
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.

Sign in to comment.

More Answers (1)

Thomas Pfau
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.
Thomas Pfau
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'])
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}
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

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!