Saving particular datapoint from an array of numbers

3 次查看(过去 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.

回答(1 个)

Matt Fig
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')
  1 个评论
SAZZAD HOSSAIN
SAZZAD HOSSAIN 2012-8-17
Thanks for the reply. I am sorry i havent made my problem entirely clear. Extending the concept of the problem you described above, for each value of t, i am actually receiving x as an array of data (1by1000). Its not just one-to-one. therefore for each t i will have a different plot. For my problem i need to apply the t with a step size of 0.01 (as you have shown), but I need to plot say just 10 plots of x from corresponding time, like t = 1:10:end, and those are the x data sets that i have to save. How can i do that?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by