MATLAB: Using a GUI with Matlab

guimatlab gui

Hi everybody, i'm just trying to create a GUI with MATLAB but i have a problem with the variable. This is my code :
if true
% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)

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


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

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2
a = get(handles.popupmenu2,'value')
% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if (a == 1)
plot(sin(0:.1:10));
end
But MATLAB answered me
if true
Undefined function or variable 'a'.
Error in fit>pushbutton1_Callback (line 269)
if (a == 1)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in fit (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)fit('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
end
Any ideas ?
Many thanks 🙂

Best Answer

Define 'a' as global variable . (Create a global variable in a GUI)
Related Question