Create & Save Off Dynamically Named Variables

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

Stephen23
Stephen23 el 24 de Ag. de 2017
Editada: Stephen23 el 24 de Ag. de 2017
"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
Richard Hopple el 25 de Ag. de 2017
I'm saving off test results where different parameters are recorded for each run. It is from the Test Manager so it is already collected as a "matlab.unittest.TestCase" object named "test".
I read the above links before I posted. The issues it raises appears to deal with dynamic variables in scripts. Almost all of what I deal with is manual. It is also a one-time event (save data to external file) during each test run and is then discarded since it is inside a function workspace (custom criteria in this case).
The Test Manager has a variable called "test" (under custom criteria) which I am saving off. I can save it off under its own unique file name 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.
Stephen23
Stephen23 el 25 de Ag. de 2017
Editada: Stephen23 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
Richard Hopple el 25 de Ag. de 2017
Primarily because, at the point it is load back into Matlab, everything will be done by hand. The saved off data is being archived. You're right about the obfuscated code being a general issue, by the way. However, at this point in the workflow, the function's workspace is closed & cleared. Any additional scripts will be hand written per each test, specifically to create figures which is an extremely manual process.
I do not want to access the meta-data. I actually want the "TestCaseResults" object located inside.
The problem I run into is that I have 35 test variables, all with different internal parameters and no easy way of figuring out what each one maps to what test case.
Stephen23
Stephen23 el 25 de Ag. de 2017
Editada: Stephen23 el 26 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:
1) Use a non-scalar structure to hold the imported data:
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.
Apologies. I didn't realize what I had typed there and gave the wrong indication of what I wanted.
What I want is to be able to is as described below.
fileName='A';
variableName='B';
save(strcat(fileName,'.mat),'variableName');
clear all
After that load file A.mat and have variable B show up in the workspace
I can't use the 3 suggestions for this specific application.
José-Luis
José-Luis el 7 de Sept. de 2017
Editada: José-Luis 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.
Stephen23
Stephen23 el 7 de Sept. de 2017
Editada: Stephen23 el 7 de Sept. de 2017
@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.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
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

function loademup(filename)
filestruct = load(filename);
result = filestruct.test;
[~, basename, ~] = fileparts(filename);
assignin('caller', basename, result)
Still not recommended though.
Richard Hopple
Richard Hopple el 7 de Sept. de 2017
Can't accept because I can't enforce downstream usage of this tool. Needs to be done at time of run
Walter Roberson
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

Iniciar sesión para comentar.

Categorías

Preguntada:

el 24 de Ag. de 2017

Editada:

el 7 de Sept. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by