フィルターのクリア

Question about 'save' option for workspace variable saving

1 回表示 (過去 30 日間)
Juster
Juster 2012 年 9 月 11 日
Hello everybody,
I've the follow problem with 'save' function in Matlab:
I would find a method that permit me to save an entire Matlab workspace but with a name coming from a value of a simulation result variable.
For example:
A = 3000;
save ('A');
In this case I got a file saved with A name and not with it's value 3000 as I would.
How can I solve it?
Thank you in advance.
Best regards.

採用された回答

Kevin Claytor
Kevin Claytor 2012 年 9 月 12 日
編集済み: Kevin Claytor 2012 年 9 月 14 日
The filename needs to be a string. You can make a string with a variable value in it by using sprintf and providing the variable;
A = 5000;
save(sprintf('My_A_Value_Is_%d',A),'A');
will save a file called 'My_A_Value_Is_5000.mat' with variable A inside of it.
Do note that you may have to change the type specifier - %d displays integers, %f floating points, etc. Your filesystem may not take kindly to having any characters in the filename.
  2 件のコメント
Juster
Juster 2012 年 9 月 14 日
編集済み: Juster 2012 年 9 月 14 日
I solved in this way:
A=5000;
B=6000;
A_string = int2str(A);
save (A_string)
In this case I can save all the variables present in the workspace (in this case A e B) but with the name 5000.mat.
@Kevin = I'm interesting in adding also a text for example before 5000.mat (ex. speed_5000.mat), but I tried with your suggestion(sprintf) but it doesn't work. Any advice?
Kevin Claytor
Kevin Claytor 2012 年 9 月 14 日
Knot; I had a typo in the save command (the second argument should have been 'A'), it should now work.

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

その他の回答 (1 件)

Matt Fig
Matt Fig 2012 年 9 月 11 日
編集済み: Matt Fig 2012 年 9 月 11 日
It always helps to read the help. If you do, you will see something like this:
save(filename,variable)
So,
save('A',A) % Make a file named A.mat that has variable A
For more information, type this:
help save
  3 件のコメント
Juster
Juster 2012 年 9 月 12 日
編集済み: Juster 2012 年 9 月 12 日
I already read the help file before writing here.
The problem is different.
I've to save a .mat file with a name taken from a variable value.
Another example could be:
pippo = 5000;
ciccio = 6000;
I would save all the .mat variables(in this case both pippo and ciccio variables) but with a name given by pippo value, so 5000.
Thanks again.
Matt Fig
Matt Fig 2012 年 9 月 12 日
Are you saying that you want to save one file per variable, and that each file will be the name of the single variable stored within? Why on earth would you want to do that, when you can simply store all the variables in one file then load them into a structure with the fieldnames given by the variable names?
Here is how to do it, but I seriously wonder why you would do this:
W = who;
for ii = 1:length(W),save(W{ii},W{ii}),end

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by