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

2 ビュー (過去 30 日間)
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

採用された回答

Image Analyst
Image Analyst 2016 年 2 月 21 日
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.

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 2 月 22 日
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 件のコメント
Arsal15
Arsal15 2016 年 2 月 22 日
編集済み: Arsal15 2016 年 2 月 22 日
Sir Walter, Thanks for your comments. I have done same as you suggested at the end.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by