How to properly convert variable names into a chain of strings in Matlab?
11 ビュー (過去 30 日間)
古いコメントを表示
I'm using the save command to store useful data of certain variables a, b_123, cc42,.... I want to have the variable names converted to something like a chain form of 'a','b_123','c42',...: chain(a,b_123,cc42,...)
so that instead of manually writing:
save(file,'a','b_123','cc42',...);
I can simply write:
save(file,chain(a,b_123,cc42,...));
Now that way if for example I want to change the name of cc42 to say cc43 (and using shift+enter to change it in all locations) I won't need to manually change it in the save command from 'cc42' to 'cc43'.
2 件のコメント
Stephen23
2019 年 8 月 17 日
From a comment: "...the fact that the names chosen for the variables may change, and it's also easier to work with when you have lots of variables"
Both of those are actually very good reaons to avoid messing around with variable names. Your data design could be significantly improved by using one structure or table, which then makes this entire problem go away.
Walter Roberson
2019 年 8 月 17 日
Not really. This is a difficulty in refactoring in MATLAB that arises because MATLAB mostly needs variables in "plain" form, but in some situations needs variable names in quoted form (especially for "save"). This problem occurs no matter what data structure you have; the best you can do is reduce down to using a single variable to reduce the number of strings you have to look for. (But that is not actually possible, as for loop control variables must be entire variables, not indexed in any way.)
採用された回答
Bruno Luong
2019 年 8 月 13 日
編集済み: Bruno Luong
2019 年 8 月 13 日
If you use number in variable name then you'll run into trouble soon or later. Just don't do it.
Not sure the advantage of using variable it-self rather typing the name directly, beside that the name might change, but then how do you know the right variable after reading back the file ?
a = 1;
b = 2;
s = chain(a,b);
save('myfile.mat', s{:});
function s = chain(varargin)
s = cell(1,nargin);
for k=1:nargin
s{k} = inputname(k);
end
その他の回答 (2 件)
Walter Roberson
2019 年 8 月 13 日
This is not possible in any released version of MATLAB.
However you can define
function savevars(varargin)
N = nargin();
Vn = cell(N,1);
Vn{1} = varargin{1}; %content!
for k = 2 : N
Vn{k} = inputname(k);
end
cmd = ['save ', strjoin(Vn, ''', ''')] ;
evalin('caller', cmd)
This is untested
4 件のコメント
Walter Roberson
2019 年 8 月 17 日
s = chain(a,b);
That stores the result of the function call in a variable.
save('myfile.mat', s{:});
and that does {:} on the stored variable. Two operations.
What would be nice is if you could do something like
save('myfile.mat', chain(a,b){:});
so that no temporary variable was needed, but that is not permitted in MATLAB.
Bruno Luong
2019 年 8 月 13 日
a = 1;
b = 2;
savevars('myfile.mat', a,b);
function savevars(file, varargin)
v = varargin;
f = cell(size(v));
for k=1:length(f)
f{k} = inputname(k+1);
end
sarg = [f; v];
s = struct(sarg{:});
save(file,'-struct','s');
end
5 件のコメント
Walter Roberson
2019 年 8 月 17 日
Even if the contents of the variable were being used intead of the names of the variables, MATLAB does "copy on write", so if you have
a = something_big;
b = something_else_big;
s = {a, b};
then s would not contain complete copies of something_big an something_else_big : a would contain a pointer to where something_big's data is stored, and b would contain a pointer to where something_else_big's data is stored, and s would contain copies of those two pointers, not the data itself.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!