How can I call the variables of MATLAB workspace from a function?
조회 수: 77 (최근 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에서 Multirate Signal Processing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!