Saving particular datapoint from an array of numbers
1 view (last 30 days)
Show older comments
Hi all, I am working on a problem where i have a variable say current, varying with time (or time steps). so at the end i will have 101 current data values for 101 time steps, which i need to save for plotting. However i am keen to save only some of the data points like 1,11,21,31........91,101 and i want to do that inside the loop, which so far i havent been able to do. Below is the way it works if i just take all the 101 values. But i dont really need that. The codes look like
timestep = 101;
for ii=1:101
-----------------
-----------------
var_list = {'x','time','current'};
filename = sprintf('TimeDependent%i.dat',ii);
for kk=1:max(size(var_list))
if(kk==1)
save(filename,var_list{kk},'-ASCII');
else
save(filename,var_list{kk},'-ASCII','-append');
end
end
end
Can anyone please suggest me a way to modify the codes above or some different way to save just those particular datapoints?
Thanks in advance.
0 Comments
Answers (1)
Matt Fig
on 17 Aug 2012
In that loop you are making 101 files, each with exactly the same data in them. I doubt this is what you want to do!
Are you just trying to save the variable for later use in MATLAB? If so, simply do this
save('MyData','x','time','current')
Then later when you want to use the data do this:
DAT = load('MyData');
Then DAT will be a struct with the fieldnames equal to your variables. For example:
clear all
t = 0:.01:100; % Create some variables.
x = sin(t);
% Now we are going to save these, the load and plot to check.
save('Mydata','x','t')
clear all % Now we clear out the workspace.
DAT = load('Mydata');
plot(DAT.t,DAT.x)
If you want to save only part of the data, simply do this:
clear all
t = 0:.01:100; % Create some variables.
x = sin(t);
% downsample the data
tn = t(1:2:end);
xn = x(1:2:end);
save('Mydat','tn','xn')
See Also
Categories
Find more on Workspace Variables and MAT-Files 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!