How to creat autoumaticly a file .mat without removing the last one

1 Ansicht (letzte 30 Tage)
rachida
rachida am 6 Sep. 2019
Bearbeitet: Adam Danz am 9 Sep. 2019
Hello !
I am doing a function when at the end i want to save the parametrers given by this function in a file .mat . The function works good but the problem is i save the new parameters in the same file wich means the last parameters are gone . so what i want is having a new file.mat with he new parametes in every use of the function, how shall i do , i am thinking of puting a name as a funnction's parameter and like that i will have a different name of the file whic means a new file but i dont know how i am using this line to creat my file
save Param.mat Param1 Param2
load Param.mat
i have these two lines at the end f my funch which taking in charge the calculation of Param1 Param2.
Thank you for your help.

Antworten (1)

Adam Danz
Adam Danz am 6 Sep. 2019
Bearbeitet: Adam Danz am 9 Sep. 2019
Here are some suggestions.
Input filename or file number
One of the function inputs can be the filename of the mat file.
function params(. . ., filename)
. . .
save(filename, 'Param1', 'Param2')
end
Similarly, if your data are organized by file numbers,
function params(. . ., fileNum)
. . .
save(sprintf('paramSet_%d.mat',fileNum), 'Param1', 'Param2')
end
Store all data in 1 mat file
If you are analyzing a population of files from a single dataset, it might be better to store the data in 1 file. I don't know how your data are organized but a structure array would be a simple and effective way of saving the data to file. Every time the function is called it would mere append the data in the mat file. You'd have to set a flag that identifies when a new mat file should be created or a filename input to identify which mat file to append.
Here's a demo by Stephen Cobeldick:

Community Treasure Hunt

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

Start Hunting!

Translated by