MATLAB Answers

saving the data in a variable

21 views (last 30 days)
C.G.
C.G. on 11 Jan 2021
Commented: Stephen Cobeldick 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')

  0 Comments

Sign in to comment.

Answers (1)

Stephen Cobeldick
Stephen Cobeldick on 11 Jan 2021
Edited: Stephen Cobeldick 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

Show 1 older comment
Stephen Cobeldick
Stephen Cobeldick on 11 Jan 2021
"I was hoping to save the variable grains for each experiment and then I could just relolad them all into matlab to plot the graph."
You can certainly save the data to files and load them when you want to plot them together, if that is your preferred approach. For that approach it is NOT required to change the variable name (in fact that would make your code more complex and less robust).
As I understand it, your script is for processing the files in one folder and you want to process multiple folders. For that you will need to use an absolute/relative file path to read the data. You could either generate them or read them from the OS using DIR (tip: you might find the '**' sytax useful for this), exactly as the first link in my answer shows.
Assuming that each grains variable is a column vector with the same number of elements, you could merge the file data like this (untested, just to get you started):
M = nan(10,16);
for k = 1:16
fnm = ... generate the filepath and filename using SPRINTF and FULLFILE
tmp = load(fnm)
M(:,k) = tmp.grains(:);
end
plot(M)
If grains is not a vector (e.g. a matrix) you need to specify how you want the data to be combined and how it should be plotted (e.g. do the different experiments need to be distinguishable (e.g. different markers or colors)), as this affects how it should be handled and plotted.
"I understand what you say about putting them into 1 matrix, but is that possible to do without saving the variable for each run, as this will be overwritten every time i process a new set of csv files?"
Personally I would convert your script into a function, call that in a loop, return the required data as an ouput argument, collect in one matrix/array as above, and finally plot that. Ultimately that would be the most reliable and efficient approach.
C.G.
C.G. on 11 Jan 2021
Thank you. Yes, I have 16 folders, all containing 3000 csv files. I want to process all these folders.
Grains is a 1x3000 cell and will be the same length for each experiment run.
Yes, I would like them to be distinguishable by different colours.
Stephen Cobeldick
Stephen Cobeldick 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!