saving the data in a variable

20 views (last 30 days)
C.G.
C.G. on 11 Jan 2021
Commented: Stephen23 on 11 Jan 2021
I have a code which imports a set of csv files from an experiment, and processes this to find out how many particles leave a system per time step.When i run the code, i create a variable 'grains', which tells me for that experiment how many particles leave through time.
I have 16 experiment runs, and I want to save the variable 'grains' for each experiment, and change the name of the variable to the name of the experiment run, so I dont have to reimport all the csv files each time I open matlab, and so I can plot them all on 1 graph for comparison.
I have tried using the save function, using the code below, but could somebody tell me how to change the name of the file when matlab saves it?
save('mfix_processing_new.mat', 'grains')

Answers (1)

Stephen23
Stephen23 on 11 Jan 2021
Edited: Stephen23 on 11 Jan 2021
"...how to change the name of the file when matlab saves it"
That is easy:
for k = 1:16
grains = ... your code
fnm = sprintf('mfix_processing_new_%d.mat',k);
save(fnm,'grains')
end
"change the name of the variable to the name of the experiment run"
Do NOT do that unless you want to force yourself into writing slow, complex, obfuscated, buggy code which is difficult to debug. Meta-data (such as experiment names or indices) is data, and data belongs in variables (not in variable names). Putting (meta-)data into variables makes it much easier and more efficient to work with.
"...so I can plot them all on 1 graph for comparison."
Rather than so complex and convoluted, the MATLAB approach is to put the data into one matrix and plot that. For example, assuming that grains is always a 10-element vector:
M = nan(10,16);
for k = 1:16
grains = ... your code
M(:,k) = grains(:);
end
plot(M)
  4 Comments
Stephen23
Stephen23 on 11 Jan 2021
"I want to process all these folders"
Search this forum for any of the thousands of questions related to looping over folders.
"Grains is a 1x3000 cell"
I do not know how to plot cell arrays, I only know how to plot numeric arrays.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!