saving the data in a variable
조회 수: 3 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Stephen23
2021년 1월 11일
편집: Stephen23
2021년 1월 11일
"...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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!