How can I call the variables of MATLAB workspace from a function?
65 次查看(过去 30 天)
显示 更早的评论
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,
采纳的回答
per isakson
2019-3-15
编辑:per isakson
2019-3-15
Do you have a good reason for doing this?
Hint:
function save_base_workspace()
evalin( 'base', 'save( ''save_base_workspace.mat'' );' )
end
7 个评论
Stephen23
2019-3-15
@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.
更多回答(2 个)
Bjorn Gustavsson
2019-3-15
编辑:Bjorn Gustavsson
2019-3-15
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 个评论
Stephen23
2019-3-15
编辑:Stephen23
2019-3-15
"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 个评论
per isakson
2019-3-17
编辑:per isakson
2019-3-18
"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]
>>
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!