Is there any way to change variable to string for the save function?

2 ビュー (過去 30 日間)
Faraz
Faraz 2016 年 4 月 27 日
コメント済み: Star Strider 2016 年 4 月 27 日
So I have many instances of Matlab running on the same drive. Every instance will calculate some result and then save it based on the data it was working on and I am really struggling with the save part.
Suppose instance 1 will be working with data ranging from 1 to 40, next will be working on data 41 to 60 and so on. The variable name within each instance is the same, so using the same name with save will just overwrite the results already present on the drive.
I can do this manually but would rather not have to worry about changing everything manually if my variables changed.
For example I have
data_start = 1, data_end = 20;
...
...
...
%result achieved, now save
my_result = sprintf(result_%d_%d, data_start,data_end); %this will create a string result_1_20
save(my_result, result);
This gives an error that argument must be a string. I have tried every possible solution, I have tried using the anonymous function
name = @(x) inputname(1);
and that didnt work, I tried using structs, and that didnt work. I even tried a variation of str2func, and that didnt work.
I am kind of out of ideas here.. the only solution available to this is eval. But I am told NEVER EVER TO USE EVAL.. so here I am.. some help would be greatly appreciated.

採用された回答

Star Strider
Star Strider 2016 年 4 月 27 日
編集済み: Star Strider 2016 年 4 月 27 日
You have three problems. The first is that the format descriptor needs to be in single quotes, the second is that you have to specify your file name as being .mat, otherwise the save function thinks your ‘my_result’ string is a variable (to which you have assigned nothing), and saves everything to the default ‘matlab.mat’ file. The third is that the arguments to save all have to be strings.
Try this:
my_result = sprintf('result_%d_%d.mat', data_start,data_end); %this will create a string result_1_20
save(my_result, 'result');
  2 件のコメント
Faraz
Faraz 2016 年 4 月 27 日
Many thanks. The single quotes missing was a typo on my part. I cant believe it was as simple as adding .mat to the name and here I was looking into eval alternatives.
Star Strider
Star Strider 2016 年 4 月 27 日
My pleasure.
The documentation on save could be a bit clearer on having the filename specifically include the .mat extension. You have to dig down into the documentation to realise that it’s necessary.

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

その他の回答 (2 件)

Kevin
Kevin 2016 年 4 月 27 日
I think you may just have a simple typo in one line of your code. Try this,
my_result = sprintf('result_%d_%d', data_start,data_end);

Jan
Jan 2016 年 4 月 27 日
編集済み: Jan 2016 年 4 月 27 日
The examples in doc save are useful. There you find the explanation that these two commands are equivalent:
save example.mat A B -v7.3
save('example.mat', 'A', 'B', '-v7.3')
As Star Stride has suggested already: Quotes are required around the name of the variable, if you use the functional form.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by