Saving particular datapoint from an array of numbers
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Matt Fig
2012-8-17
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')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!