I have several matrices that I would like to save in a loop. I've tried to read all the save documents but it doesnt seam to work. I can save the matrix using a string and it becomes like A150.mat but I also want the matrix inside to be called that as well. The problem is that save() saves the matrix after what its called so when I get the name as a string(imported from a document outside matlab) its impossible to give the matrix that name. Thanks for any help

 採用された回答

Kevin Holst
Kevin Holst 2012 年 2 月 3 日

1 投票

can you not import the string prior to saving? something like:
...
str = importFun(file);
eval([str ' = matToSave;'])
save([str '.mat'],str)
eval(['clear ' str])
clear str

3 件のコメント

Kevin Holst
Kevin Holst 2012 年 2 月 3 日
I should add, thanks to Sean for the reminder about the dangers of eval, that if you somehow get str = 'clear all', you're hosed, so you might want to put a check in there to make sure that the str is actually a variable.
exist(str,'var')
Also the last two lines should just be:
clear(str,'str')
Jan
Jan 2012 年 2 月 4 日
There can be more evil problems then "eval('clear all')":
str = 'x; system(''format C:'')'
eval(['clear ', str])
;-)
Emil
Emil 2012 年 2 月 6 日
Thank you, it worked perfectly! :)

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

その他の回答 (1 件)

カテゴリ

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by