Using name of saved matrix

3 visualizaciones (últimos 30 días)
Ted Baker
Ted Baker el 22 de Abr. de 2020
Comentada: Ted Baker el 22 de Abr. de 2020
Hi,
I'm trying to plot multiple outputs from a script on the same graph. As it stands, my script outputs the array I want to plot in a variable called "diff". I ran this script three times, changing my input data, and after each run I right clicked the variable "diff" and saved it as a .mat file with a different name (ie data1.mat, data2.mat, data3.mat). After clearing the workspace, I attempted to import these three .mat files into the workspace again (by dragging and dropping from file directory). However, I noticed that each time it imports the file with the variable name "diff" and not the file name. This means I only have one variable in my workspace and I cant plot the three lines as I expected. Is there an easy way of importing the variable with a new name each time, if it detects that it already exists? Or am I going about this all wrong?
Thanks in advance.

Respuesta aceptada

Johannes Hougaard
Johannes Hougaard el 22 de Abr. de 2020
I think you should go a different route where you don't save your variables to .mat files and simply keep the variables in your MATLAB workspace without clearing.
run('script'); % execute your script however you like to
data1 = diff;
run('script'); % execute your script again however you like to
data2 = diff;
run('script'); % execute your script again however you like to
data3 = diff;
plot([data1;data2;data3]');
Or to rename you variable diff (in the script) before running it the second time and third time (e.g. to diff2/diff3)
If you have to store the data in files you could specify an output to the load call
data1 = load('data1.mat')
data2 = load('data2.mat')
data3 = load('data3.mat')
plot([data1.diff;data2.diff;data3.diff]')
  2 comentarios
Steven Lord
Steven Lord el 22 de Abr. de 2020
In addition to what Johannes Hougaard wrote, diff already has a meaning in MATLAB. If you accept that you're not going to be able to call the diff function while a variable named diff exists, you can use that name but usually we recommend avoiding the names of functions in MATLAB when naming your variables.
Ted Baker
Ted Baker el 22 de Abr. de 2020
Thanks Johannes, much appreciated. It works now.
Thank you also, Steven, for letting me know about diff!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Standard File Formats en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by