1
votes

I am new to Matlab GUIs. I'm having a strange problem. When running the GUI code from the matlab code editor (clicking the run button), everything runs fine. However, when I double click the file from matlab (file browser on the left) I get an error.

Here's what Matlab says:


Attempt to reference field of non-structure array.

Error in GUI>scanButton_Callback (line 81) axes(handles.axes1);

Error in gui_mainfcn (line 96) feval(varargin{:});

Error in GUI (line 42) gui_mainfcn(gui_State, varargin{:});

Error in @(hObject,eventdata)GUI('scanButton_Callback',hObject,eventdata,guidata(hObject))

Error while evaluating uicontrol Callback


I am trying to plot a graph that is created by the 'main' script in 'axes1' in the GUI. The only change I've made in the GUI code is:


% --- Executes on button press in scanButton.

function scanButton_Callback(hObject, eventdata, handles)

% hObject handle to scanButton (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

axes(handles.axes1);

main();


The code in the 'main' script that creates the plot is:


[x, y, z] = meshgrid(1:cSecDim, 1:cSecDim, 1:numSec);

scatter3(x(:),y(:),z(:),30,fullShape(:), 'fill');


I am sure there is a simple answer as to why the error is occurring. Please forgive my ignorance.

1

1 Answers

0
votes

Double-clicking on the GUI fig file does not "run" the GUI. It just launches the figure that corresponds to the GUI without doing any of the initializations of variables (in particular handles) that would normally occur.

As far as I'm aware, there are only three ways to run the GUI - through GUIDE (if using this tool to design the GUI), through the MATLAB Editor (as you have done), or from the Command Window by typing in the name of the GUI. For example, if your GUI m- and fig-files are named myGui.m and myGui.fig, you can run this GUI from the command line as

myGui

There is no need for any extension.