Calling an m file from GUI, and using data in workspace

4 次查看(过去 30 天)
Hi, I am using 2 pushbuttons in a GUI designed using GUIDE. First button is to open UI for browser .mat files which include 'structures' gets successfully loaded into the base workspace.
With the second pushbutton, I wish to call an .m file which has the code to work on the data in the workspace. The code executes as expected if run by itself, and writes out an excel sheet.
However, when I execute it through pushbutton, lines of code in the called .m files that try to access the data in workspace is called as 'undefined variable', even though I can still see those variables in the workspace. I do not have clear/clear all anywhere in the code.
***************** First pushbutton - loads data in workspace
% --- Executes on button press in Load_Data.
function Load_Data_Callback(hObject, eventdata, handles)
% hObject handle to Load_Data (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[file_name1,file_path1] = uigetfile({'*.*','.mat Files'})
total_path1 = strcat(file_path1,file_name1)
% evalin('base','load(total_path1)') --- this did not work!
fid=load(total_path1);
names=fieldnames(fid,'-full');
p=length(names)
for count1=1:p
namevar=strcat('fid.',names{count1});
tempvar=eval(namevar);
assignin('base',names{count1},tempvar);
end
%******************* Second pushbutton - to call m files
% --- Executes on button press in Calculate.
function Calculate_Callback(hObject, eventdata, handles)
% hObject handle to Calculate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles=guidata(hObject);
VVS_Function_15A %%Name of m file
guidata(hObject,handles);
****************** Error Message
Undefined variable "AutoPowerSpectrum_0" or class
"AutoPowerSpectrum_0.y_values.values".
Error in VVS_Function_15A (line 11)
Crr_acc = AutoPowerSpectrum_0.y_values.values(:,:); % APS Acceleration
Error in GUI_1>Calculate_Callback (line 155)
VVS_Function_15A
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in GUI_1 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)GUI_1('Calculate_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
I have a day's experience with GUI, and I am not familiar about workspaces. Is there a way to resolve this?

采纳的回答

Shameer Parmar
Shameer Parmar 2016-6-9
Hi Aniket,
I understand your problem with handling GUI.
First tell me, is your 'VVS_Function_15A.m' file is having function? (just open your .m file and check if it is started with keyword 'function' ?)
If the function file is there, then it uses its own workspace for execuation of data\logic (and it does not have direct access to 'base' workspace) and because of this it is not able to find that object 'AutoPowerSpectrum_0' into its own workspace and it is throwing an error.
to access that object from 'base' workspace you need to use the 'evalin()' command before line # 11 of your .m file.
Try for this in .m file:
function VVS_Function_15A()
variableList = evalin('base','who');
for i=1:length(variableList)
tempVariable = evalin('base',variables{i});
Here add Your logic from line # 11 till you are reading the information of parameter tempVariable.
Note: use tempVariable instead of actual parameter name.
Ex: Crr_acc = tempVariable.y_values.values(:,:)
and so on...
end
end
  2 个评论
Aniket Parbat
Aniket Parbat 2016-6-9
Thanks a lot Shameer! It works!! For future reference, it has to be variableList{i} instead of variables{i} in line 6 of your recommended code.
I was fortunate there were only 4 structure variables in the mat files, so I could assign the tempVariable inside your for loop as ,
if i==1
Struct_APS0_acc = tempVariable;
elseif i ==2
Struct_APS1_pr = tempVariable;
elseif i==3
Struct_CPS0_acc = tempVariable;
elseif i==4
Struct_CPS1_pr = tempVariable;
%%Since I have all my variables loaded, I have added the code here
end
If there are a lot of variables, is there a faster/better way to handle the assignment other than by if/else or switch-case?
Stephen23
Stephen23 2016-6-9
编辑:Stephen23 2016-6-9
"If there are a lot of variables, is there a faster/better way to handle the assignment"
Yes, use a structure to hold them all.
Also note that the documentation explains the best ways to pass data between callbacks:
The fastest and most reliable methods are to pass the data properly using guidata or handle userdata. You should avoid using evalin, which is buggy and slow (no matter how much beginners love using it):
Ways of passing data between workspaces are explained here:
Note that evalin and assignin are the least recommend methods of passing data between workspaces.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by