Borrar filtros
Borrar filtros

How do I dynamically generate a file name for save in MATLAB

132 visualizaciones (últimos 30 días)
Doug Hull
Doug Hull el 18 de En. de 2011
Comentada: Ali Fares el 31 de En. de 2021
I do not know the file name at the time of coding, only at run time.

Respuesta aceptada

Doug Hull
Doug Hull el 18 de En. de 2011
You're probably trying
fname = 'foobag';
save fname variable;
To do this correctly, you need to use the "functional" form of save:
fname = 'foobar';
save(fname, 'variable');
In fact, it is true in general that the following two lines are equivalent:
command str1 str2 str3
command('str1', 'str2', 'str3')
This allows one replace any or all of the parameters with dynamically generated strings. This is also useful in commands like PRINT, LOAD, CLEAR, etc.
[From the MATLAB FAQ of Ancient Times]

Más respuestas (2)

Anish
Anish el 18 de En. de 2011
You can use the function form of the SAVE command:
i = 10;
fname = sprintf('myfile%d.mat', i);
save(fname)
For more help, see:

Neha  Singh
Neha Singh el 20 de Abr. de 2017
How it is possible to save without loop variable? Suppose I took 'x' variable and I want to append at the end of file while saving i.e filex. This show result into file1, file9 somthing like this.

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by