save variables in .mat files with desired name.

219 ビュー (過去 30 日間)
Sunil  Shahi
Sunil Shahi 2013 年 8 月 11 日
コメント済み: Eleanna Kritikaki 2020 年 1 月 28 日
I have a function where I do my calculation to get A cell or array. Now I want to save this array in a .mat file but I want to change the variable name.
myData = [1 2 3 4 5 6 7 8 9]; %data from my calculation
save Data.mat myData
This saves my data in Data.mat file but with variable name myData. Now I want to save values of myData in .mat file but I want to save the value under a variable name given via input function. Something like:
myData = [1 2 3 4 5 6 7 8 9]; %data from my calculation
newName = input('I want to save the variable under the name:', 's');
save Data.mat ???
I hope that make sense
Thanks

採用された回答

Jan
Jan 2013 年 8 月 12 日
編集済み: Jan 2013 年 8 月 12 日
myData = [1 2 3 4 5 6 7 8 9]; %data from my calculation
newName = input('I want to save the variable under the name:', 's');
S.(newName) = myData;
save('Data.mat', '-struct', 'S') % EDITED
  3 件のコメント
Richard Crozier
Richard Crozier 2016 年 6 月 17 日
Does this solution result in making a copy of the data in memory, or is matlab smart enough to avoid this?
John Paden
John Paden 2018 年 11 月 30 日
No it is does not copy. Matlab uses copy-on-write and does not make a copy unless changes are made to one of the variables. You can do any number of copies between variables, structure fields, or cell array elements and only a pointer to the original data is used to store the "copy". When any one of the copies is written to, the copy that is written to will be fully copied at that point, but all the other copies that are not written to will continue to point back to the original copy. For example, the following code only stores a single copy. The first line of code actually uses memory to store the variable. The next four lines are just pointers back to the original data.
>> test = zeros(250e6,1);
>> s.data = test;
>> C{1} = test;
>> C{2} = test;
>> C{3} = s.data;

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

その他の回答 (2 件)

Bruno
Bruno 2019 年 7 月 25 日
Maybe, a simples way to answer that need is to do the following:
fname = sprintf('Data.mat', myData);
save(fname)
Hope it can help others.
  1 件のコメント
Eleanna Kritikaki
Eleanna Kritikaki 2020 年 1 月 28 日
Damn right it did. Thanks man

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


Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 11 日
myData = [1 2 3 4 5 6 7 9];
newName = input('I want to save the variable under the name:', 's');
assignin('base',newName,myData)
save('Data.mat',newName)
  3 件のコメント
Jan
Jan 2013 年 8 月 12 日
Avoid to create variables dynamically by assignin or eval. There is always a better solution.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 12 日
myData = [1 2 3 4 5 6 7 8 9]; %data from my calculation
newName = input('I want to save the variable under the name:', 's');
a.(newName)=myData;
save('Data.mat','-struct','a',newName)

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by