saving variables programmatically using list of variable names
92 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
checker
el 5 de Jun. de 2020
Comentada: checker
el 5 de Jun. de 2020
Hi all,
This should be simple but I think my frustration level is keeping me from seeing the path.
I simply want to save some variables on the workspace based on a list of those variable names.
a=1;b=2;c=3
varNames = {'a','b','c'};
save('myMatFile',varNames)
which yields the error 'Error using save. Must be a string scalar or character vector'. (what must be a string scalar? File name? vars? Yes I know, vars, but still TMW...)
so how about
save('myMatFile',cell2mat({'a,','b,','c'});
yielding the more useful error 'a,b,c' is not a valid variable name.
I see where this is going, I must have a properly formatted string array akin to eval inputs. But there really should be an easier way, like my original attempt.
Thanks for the support and letting me rant a bit.
-Chris
1 comentario
Steven Lord
el 5 de Jun. de 2020
That message does seem vague. I've reported that to the development staff.
Respuesta aceptada
Stephen23
el 5 de Jun. de 2020
Editada: Stephen23
el 5 de Jun. de 2020
The simple solution is to use a comma-separated list:
varNames = {'a','b','c'};
save('myMatFile',varNames{:})
% ^^^ you need this!
Tip: when load-ing the file data you should use an output variable (which is a scalar structure):
S = load(...);
Más respuestas (1)
Sulaymon Eshkabilov
el 5 de Jun. de 2020
Editada: Sulaymon Eshkabilov
el 5 de Jun. de 2020
Here is an easy solution:
a=1;b=2;c=3;
save('myMatFile.mat','a','b','c')
% Or
a=1;b=2;c=3;
MY_var = {'a','b','c'};
save('myMatFile.mat','MY_var')
1 comentario
Ver también
Categorías
Más información sobre Variables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!