How can I call the variables of MATLAB workspace from a function?
84 views (last 30 days)
Show older comments
I am trying to make a .mat file from withing a functioin that contains all the variables in the MALTAB workspace but when I attempt to do that from inside a function using save('NAMEWORKSPACE'); it saves all the variables from the workspace of the function in the .mat file and I dont want that.
How can create a .mat file with the variables from my MATLAB workspace from inside a function?
Please advice,
2 Comments
Accepted Answer
per isakson
on 15 Mar 2019
Edited: per isakson
on 15 Mar 2019
Do you have a good reason for doing this?
Hint:
function save_base_workspace()
evalin( 'base', 'save( ''save_base_workspace.mat'' );' )
end
7 Comments
Stephen23
on 15 Mar 2019
@Alberto: all of this is an incredibly complex hack to fix that badly designed MDF import function (design flaw: importing straight into the base workspace). The simplest solution is to avoid this in the first place, which you can trivially do by using a different MDF import function, see my answer for more information.
More Answers (2)
Bjorn Gustavsson
on 15 Mar 2019
Edited: Bjorn Gustavsson
on 15 Mar 2019
I suppose you can build something with the matlab-function evalin, Something like:
savename = 'whatever.mat';
savecmd = ['save(',savename,')'];
evalin(savecmd,'base')
(Snippet not tested...)
But this really looks like an odd idea, for what purpose do you need this? The workspace have exactly the same variables before you call the function as it has when you reach that save-attempt - so why not save the variables before calling the function. And if the assertion in the previous sentence is not true because you use a lot of global variables or rely on assignin, then you most ceratainy make yourself terrible problems for the future.
HTH
0 Comments
Stephen23
on 15 Mar 2019
Edited: Stephen23
on 15 Mar 2019
"I don't seem to be able to do it when I am inside a funtion and don't know the name of the variables prior to running the program."
This is because Stuart Garrity's submission has a flawed design, because it magically creates numbered variables in the base workspace. This makes the variables slow, complex, and difficult to access programmatically:
You can avoid this entire problem by simply using Erin McGarrity's FEX submission, which imports the data into an output variable:
Simply call the function inside the workspace where you want it, and you avoid the entire problem of your original question:
data = importMDF3(...)
4 Comments
per isakson
on 17 Mar 2019
Edited: per isakson
on 18 Mar 2019
"I have downloaded importMDF3" I assume you mean MDF_Reader.zip, which contains the three files you mention plus a private folder, which in turn contains twentyfive files.
"Undefined function or variable 'mdfinfo' and ... 'parseparameters'." These two are files in the private folder. Hence, MDF_Reader.zip is not "installed" correctly.
Matlab has a toolbox named Vehicle Network Toolbox, which includes tools to handle Measurement Data Format (MDF).
I have successfully run importMDF3 with an example file from the Matlab toolbox.
>> z3 = importMDF3('c:\Program Files\MATLAB\R2018b\examples\vnt\ADAC.dat')
Created 4 signal variable(s) appended with '_1' for 'cg comment' rate
... and 1 actual time vector 'time_1'
z3 =
1×1 cell array
{1×1 struct}
>>
>> z3{1}
ans =
struct with fields:
time_1: [83140×1 double]
Current_1: [83140×1 double]
Power_1: [83140×1 double]
Velocity_1: [83140×1 double]
Voltage_1: [83140×1 double]
>>
See Also
Categories
Find more on Multirate Signal Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!