How can I call the variables of MATLAB workspace from a function?

조회 수: 77 (최근 30일)
Alberto
Alberto 2019년 3월 15일
댓글: Alberto 2019년 3월 18일
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
KSSV
KSSV 2019년 3월 15일
Not clear..you have confused us.
Alberto
Alberto 2019년 3월 15일
I am sorry for the confusion.
I understand that every funtion has its own workspace and save('variables'); stores all the variables in the workspace into a .mat file. I am trying to create a .mat file from inside a function that doesn't stores all the variables from the workspace of the function but rather it stores all the variables from the workspace that appears next to the command window.

댓글을 달려면 로그인하십시오.

채택된 답변

per isakson
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
Alberto
Alberto 2019년 3월 15일
편집: per isakson 2019년 3월 18일
The reason why I am trying to send all the information to a .mat file is to access the information inside variables easier, 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.
Explanation:
The program calls the mdfimport from inside a funtion (once I press a button in a GUI) and imports all the MDF-Data to the base workspace, the program then send the names of all the variables in the workspace to a listbox in the GU as followsI:
set(handles.unselectedlist,'String',evalin('base','who'));
Then the user needs to move to another list all the names of the variables (as Strings) that he wants to analyse in order to later be plotted.
selectedDataStr = handles.selecteddata.String % struc with the selected names of variables inside the GUI
After the user has selected the data I would like to plot it.
The problem is that inside selectedDataStr I only have strings with the name of all the variables and getting access to the data inside the variables in the base workspace seems to me impossible only using selectedDataStr. I tried to do it this way:
workspace_variables = evalin('base','who');
for i = 1:length(selectedDataStr)
for j = 1:length(workspace_variables);
if strcmp(selectedDataStr(i),workspace_variables(j))
eval(selectedDataStr(i),'workspace_variables(j)');
% the reason why this will not work is because 'workspace_varibales(j)' doesn't exist in the base workspace. I hoped that Matlab would read 'workspace_variables(j)' as an arrray with strings inside but it does not.
end
end
end
The final goal is to get easy access to the values inside the variables in the base workspace, for that reason I have consider the option of creating a .mat file when calling the function mdfimport in order to get easy access to the value of the variables. Please advice.
Stephen23
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
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

Stephen23
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
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]
>>
Alberto
Alberto 2019년 3월 18일
Thank you for all the advice! It works perfectly now.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by