Create & Save Off Dynamically Named Variables
Mostrar comentarios más antiguos
So I'm trying to both make and save off a dynamically named variable. I realize that dynamic named variables are bad and hard to use, however, currently I have 5 different versions of test and it's only going to grow. End goal is to load a variable name that makes sense into the workspace. Something like:
fileName='meaningfulFileName';
variableName='meaningfuleVariable';
save(strcat(fileName,'.mat),'variableName');
clear all
then load the file meaningfulFileName.mat and have meaningfulVariable show up in the workspace.
On a side note, these files are being saved off for archive purposes mostly & I don't want the next person to have to remember to change the variable name as soon as they load it.
Thanks in advance!
8 comentarios
"I don't want the next person to have to remember to change the variable name as soon as they load it."
What? Can you explain the logic of that? How does using different variable names resolve that problem? By having different variable names requires changing the code to use different variables... what is the point of that? Why not simply use one variable name, then no one has to remember anything.
In any case, the simplest and most reliable answer is to load into a structure:
S = load(...);
S.variableName % access the data
and then you can access all the data to your hearts content. Why do you want to make your life more complex than that?
You might like to read this:
and also the MATLAB documentation:
Richard Hopple
el 25 de Ag. de 2017
"The issues it raises appears to deal with dynamic variables in scripts."
Actually the problems are not particular to scripts or functions. The problems are about dynamically creating and accessing variable names, particularly in a loop, and how buggy, slow, and obfuscated that code is. Which is, strangely enough, what the title and introduction explain.
"..but when I load it back into Matlab's workspace, it is loaded as "test". I want the Matlab workspace variable to display as the file name."
Why? Just to make your code more complicated, slower, and buggier than it needs to be? test seems like a reasonable name for a variable.
I suspect that you have some meta-data in the filename and want to access this when importing the data. Note that putting meta-data into variable names is a very bad idea as it makes accessing that meta-data very slow, fragile, and complex. Much simpler would be to store the meta-data as data in its own right, e.g.:
test = importdata(...) % your data
meta = ... % whatever from the filename
This will be much simpler, more efficient, and easier to debug than any hack code using dynamic variable names.
Richard Hopple
el 25 de Ag. de 2017
What class of object are these data? What file format are you using to store them in? Actually telling us concrete details would help.
Some ideas for you:
S(1).name = 'file1.mat';
S(1).data = ...
S(2).name = 'file2.mat';
S(2).data = ...
You can then easily access the fields using the methods shown in the documentation:
cellOfNames = {S.name}
2) Store the objects in a cell array, and the meta-data as well:
names{1} = 'file1.mat';
names{2} = 'file2.mat';
...
data{1} = load('file1.mat');
data{2} = load('file2.mat');
3) if the objects that you mention are actually structures with the same fields, then you can easily load them into a non-scalar structure:
S(1) = load(...)
S(2) = load(...)
and add any meta-data that you so desire:
S(1).name = 'file1.mat';
S(2).name = 'file2.mat';
4) Use dynamic field names:
S.('VW') = ...
S.('ford') = ...
S.('toyota') = ...
etc. "I do not want to access the meta-data."
The filenames or paths must be different, and therefore contain some meta-data (whether it be an index, a test name, parameter values, or something else). You wrote that "I want the Matlab workspace variable to display as the file name.", so you clearly stated that you do want to use this meta-data.
Richard Hopple
el 7 de Sept. de 2017
Late to the party:
From what I could get, you need a variable B to exist in your workspace, probably because you have a script that needs it.
The question is then, can't this be solved by making your script a function that takes the file name as an argument. You can then do the pertinent checks to see that you loaded what you expected and name it whatever you want.
I've read the comments here and can't really understand why you want to avoid loading into a structure.
@Richard Hopple: your explanation makes no sense: there is no variable B. Your example has a variable named fileName and one named variableName, both of which contain strings. So when you say that you want to "variable B show up in the workspace", what are you referring to? There is no variable B. What is variable B?
In any case, you would be much better of converting all scripts into functions and passing the data properly as input and output arguments, then you avoid this whole problem entirely.
Even if the scripts cannot be changed (e.g. they are provided by an obnoxious professor who insists that they know how to write code properly and isn't COBOL just wonderful) you can still avoid the whole problem by calling the scripts from inside some function:
function [X,Y] = myfun(A,B,C)
script_using_ABC
end
and then you can call that function with the loaded data and the names of the loaded data do not matter:
S = load(...);
[G,H] = myfun(S.blah, S.data, S.whatever)
Trying to magically rename variables is not a robust or efficient solution. Don't get stuck one some idea that your code is perfect and that scripts are brilliant. They aren't, no matter how much some beginners like them. You would have avoided this entire issue if you had written better code right form the start (i.e. functions, not scripts), and not wasted weeks waiting for a solution to a pointless problem.
Respuestas (1)
Walter Roberson
el 24 de Ag. de 2017
Provide a small routine that loads files for the user under the variable name.
function result = loademup(filename)
filestruct = load(filename);
fn = fieldnames(filestruct);
result = filestruct.(fn{1});
If this is too hard for the user to user, then
function loademup(filename)
filestruct = load(filename);
fn = fieldnames(filestruct);
result = filestruct.(fn{1});
assignin('caller', 'meaningfuleVariable', result)
3 comentarios
Walter Roberson
el 25 de Ag. de 2017
function loademup(filename)
filestruct = load(filename);
result = filestruct.test;
[~, basename, ~] = fileparts(filename);
assignin('caller', basename, result)
Still not recommended though.
Richard Hopple
el 7 de Sept. de 2017
Walter Roberson
el 7 de Sept. de 2017
Editada: Walter Roberson
el 7 de Sept. de 2017
fileName='A'; variableName='B';
datastruct.(variableName) = name_of_variable_data_is_in;
save(strcat(fileName,'.mat),'datastruct', '-struct');
clear all
Now when someone does a plain load of the mat file, the data will appear in the variable B
Categorías
Más información sobre Workspace Variables and MAT Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!