Calling an m file from GUI, and using data in workspace
Show older comments
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?
Accepted Answer
More Answers (0)
Categories
Find more on Workspace Variables and MAT Files 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!