How to save variables with in the function with a new file name with every iteration.

2 visualizaciones (últimos 30 días)
I tried to save my work space with a different file name for every iteration but it doesn't save any variable inside the function.? I want to save the variables of the function in every iteration.
As I am calling a function Hexagon so it runs for 20 seconds and it generates some output so I want to save work space of Hexagon function with a file name start with MS_Num and then MS_Num value every time it takes.
clc
clear all
close all
sizeL = 100;
radius = 10;
Num_MS = [1500 2000 3000 4000 5000 6000 7000 8000 9000 10000];
for i = 1 : 10
MS_Num = Num_MS(i);
Hexagon(sizeL,radius,MS_Num);
end
I will be happy if you can guide me. Thanks

Respuesta aceptada

Image Analyst
Image Analyst el 21 de Feb. de 2016
Well you're not constructing a file name at all, and of course you're not then using that file name in a call to save() at all, so it's not surprising that no variables get saved to a file. Please see the FAQ for sample code to do what you want:
You'll see you can call sprintf() inside the loop to create a filename, then use save
for i = 1 : 10
MS_Num = Num_MS(i);
Hexagon(sizeL,radius,MS_Num);
thisBaseFileName = sprintf('MS_Num%d.mat', MS_Num);
fullFileName = fullfile(pwd, thisBaseFileName);
save(fullFileName, 'var1', 'var2', 'var3'); % Whatever variables you want.
end
Replace 'var1', etc. with the names of whatever variables you want to save in the mat file.

Más respuestas (1)

Walter Roberson
Walter Roberson el 22 de Feb. de 2016
As I wrote earlier,
"You cannot save the workspace of a different function than you are running in, not unless that other function is your immediate caller"
This means that once you call Hexagon() and it returns, the workspace of the Hexagon function is gone, completely destroyed, and there is no way of getting it back after the call in order to save its variables. If you want to save the variables of the Hexagon function, you need to call save() inside Hexagon() [or in some routine called by Hexagon]
  1 comentario
Arsal15
Arsal15 el 22 de Feb. de 2016
Editada: Arsal15 el 22 de Feb. de 2016
Sir Walter, Thanks for your comments. I have done same as you suggested at the end.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by