필터 지우기
필터 지우기

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
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
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
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
  댓글 수: 1
tensorisation
tensorisation 2019년 8월 17일
편집: tensorisation 2019년 8월 17일
I just gave an example of variables, the names themselves are meaningless. The main reason for this is due to 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.
Note sure what exactly do you mean by "how do you know the right variable after reading back the file ?"
The method you suggested here appears to be working and is probally what I'm looking for, I just need to make sure it is working properly.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
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
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.
tensorisation
tensorisation 2019년 8월 17일
편집: tensorisation 2019년 8월 17일
Agreed, it would be nicer if I could do this without the need of creating a temporary cell in-between, but other than that it's quite simple and it works. It's good enough for now I guess. Thanks for the input.

댓글을 달려면 로그인하십시오.


Bruno Luong
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
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.
tensorisation
tensorisation 2019년 8월 17일
편집: tensorisation 2019년 8월 17일
Thanks for the clarification Walter.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by