Saving particular datapoint from an array of numbers

1 vue (au cours des 30 derniers jours)
SAZZAD HOSSAIN
SAZZAD HOSSAIN le 17 Août 2012
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.

Réponses (1)

Matt Fig
Matt Fig le 17 Août 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')
  1 commentaire
SAZZAD HOSSAIN
SAZZAD HOSSAIN le 17 Août 2012
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?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by