Clear Filters
Clear Filters

How to properly convert variable names into a chain of strings in Matlab?

11 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 17 Aug 2019
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 on 17 Aug 2019
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.)

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 13 Aug 2019
Edited: Bruno Luong on 13 Aug 2019
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 Comment
tensorisation
tensorisation on 17 Aug 2019
Edited: tensorisation on 17 Aug 2019
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.

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 13 Aug 2019
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 Comments
Walter Roberson
Walter Roberson on 17 Aug 2019
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 on 17 Aug 2019
Edited: tensorisation on 17 Aug 2019
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.

Sign in to comment.


Bruno Luong
Bruno Luong on 13 Aug 2019
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 Comments
Walter Roberson
Walter Roberson on 17 Aug 2019
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!