saving the data in a variable

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')

回答 (1 件)

Stephen23
Stephen23 2021 年 1 月 11 日
編集済み: Stephen23 2021 年 1 月 11 日

0 投票

"...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 件のコメント

C.G.
C.G. 2021 年 1 月 11 日
Thank you for your response. I think I understand what you are saying.
I have copied my whole code below to see if that helps my explanation. I have 16 folders all with different csv files in, each representing 1 experiment. My code processes this and plots a grains vs. time graph for each set of files.
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.
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?
%% input
%input data from each .csv file into matlab
%need particle id, velocity and coordinates stored
files = dir('*.csv'); %dir lists the files in a folder. In the specified folder, recognise all the .csv files
num_files = length(files); %specify how many files have been found and call it num_files
particledata = cell(length(files), 1); %create a cell array the same length as the number of files in one column
%for all the files found in the specified folder, read the tables of data and fill the empty cell array 'results' with the data in each .csv file
for a = 1:num_files
particledata{a} = readtable(files(a).name);
end
%% time steps
%number of csv files = number of time steps the model is run for (seconds)
totalruntime = num_files;
%% particles leaving the rice pile
%for each .csv file (cell in the array), calculate the number of particles after a certain x coordinate
%-0.15 is the x coordinate for the end of the box on the mfix model
ricepileoutlet = -0.15;
%for all the cells in 'particle data'..
for b = 1:length(particledata)
%save all the rows in the 5th column (x-coordinates) of each cell as a new variable x
x{b} = particledata{b}(:,5);
%use the function table2array to turn the format of the data from a table to an array
x_array{b} = table2array(x{b});
%for all the rows in the array, if the x coordinate goes beyond the outlet
%of the rice pile, display 'grain left rice pile'
if x_array{b}<ricepileoutlet
disp('grain left rice pile')
end
%sum the total number of grains leaving the rice pile in each cell, and save into a new variable 'grains'
grains{b}=sum(x_array{b}<ricepileoutlet);
fprintf('A total of %d grains left the rice pile\n',grains{b});
end
%% plots
time = 1:totalruntime;
%convert the cell file to a double file to allow plotting of data
grains = cell2mat(grains);
%mass efflux vs. time
%plot 'grains left rice pile' vs. time step as a bar graph
%0.01 sets the width of each bar
figure(1)
bar(time,grains, 0.01, 'EdgeColor', 'r')
title('mass efflux')
xlabel('Time (S)')
ylabel('Mass Efflux (No. of Particles)')
Stephen23
Stephen23 2021 年 1 月 11 日
編集済み: Stephen23 2021 年 1 月 11 日
"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. 2021 年 1 月 11 日
編集済み: C.G. 2021 年 1 月 11 日
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.
Stephen23
Stephen23 2021 年 1 月 11 日
"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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

タグ

質問済み:

2021 年 1 月 11 日

コメント済み:

2021 年 1 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by