Borrar filtros
Borrar filtros

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

11 visualizaciones (últimos 30 días)
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 comentarios
Stephen23
Stephen23 el 17 de Ag. de 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 el 17 de Ag. de 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.)

Iniciar sesión para comentar.

Respuesta aceptada

Bruno Luong
Bruno Luong el 13 de Ag. de 2019
Editada: Bruno Luong el 13 de Ag. de 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 comentario
tensorisation
tensorisation el 17 de Ag. de 2019
Editada: tensorisation el 17 de Ag. de 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.

Iniciar sesión para comentar.

Más respuestas (2)

Walter Roberson
Walter Roberson el 13 de Ag. de 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 comentarios
Walter Roberson
Walter Roberson el 17 de Ag. de 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 el 17 de Ag. de 2019
Editada: tensorisation el 17 de Ag. de 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.

Iniciar sesión para comentar.


Bruno Luong
Bruno Luong el 13 de Ag. de 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 comentarios
Walter Roberson
Walter Roberson el 17 de Ag. de 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.
tensorisation
tensorisation el 17 de Ag. de 2019
Editada: tensorisation el 17 de Ag. de 2019
Thanks for the clarification Walter.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by