Matlab: GUI Interface
Generating a GUI interfaces
(German) In this post I show you how to generate a GUI interface with Matlab with buttons, checkboxes ...There one has to write "guide" in the console of Matlab and a window opens where you can create a new interface or load an old one.
Now an editor opens where you easily can put the objects you want in. You can safe it an then the Matlab code is generated.
To program it, it is important that you relate to each object an "tag". This you can do by double click on the object. An Inspector opens where you can edit it an other things like the "value", "name" or the design:
Now one can push the green arrow to generate the code where you can define your functions:
Programmieren des GUI interfaces
The button can be programmed in the code under "function pushbutton1_Callback(hObject, eventdata, handles)". In this case the program generates a coil in "axes1" in dependence of the value in the text field or of the slide (between 0 and 3 pi). With global one can define a global variable which you can use everywhere in your code if you write on every function at the beginning "global x".function pushbutton1_Callback(hObject, eventdata, handles)
global x;
xneu = 1080*get(handles.slider1,'Value')
x = [0:0.1:xneu/180*pi];
To plot you chose the "axis" of the graph "axes1":
axis(handles.axes1);
and then plot it:
plot(sin(x).*x,cos(x).*x,'parent',handles.axes1);
guidata(hObject,handles);
next thing is to get the value of "checkbox" with "get(handles...)"
check = get(handles.checkbox1,'Value');if check == 1
axis(handles.axes2);
plot(x,cos(x).*x,'parent',handles.axes2);
guidata(hObject,handles);
end
If the checkbox is activated, in "axes2" is a function plottet too:
For "slider" and "editbox" the Value "hObject" is set with "set(handles...)" to the corresponding to the same value, that they both have the same value all the time:
function slider1_Callback(hObject, eventdata, handles)
slider = num2str( round(get(hObject,'Value')*1080,2) );
set(handles.edit1,'String', slider);
...
function edit1_Callback(hObject, eventdata, handles)
edit = round(str2num( get(hObject,'String') ),2);
set(handles.slider1,'Value',edit/(1080));
Helpfull is the call the button everytime you use the slider, checkbox or editbox, that you don't have to push everytime pushbutton1:
pushbutton1_Callback(hObject, eventdata, handles);
All other objects in the Matlab GUI are very similar and easy to handle with the "callback" function.
Source code:
function varargout = GUI(varargin)gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_OpeningFcn, ...
'gui_OutputFcn', @GUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function GUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = GUI_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% this is the buttom that shows the figures
function pushbutton1_Callback(hObject, eventdata, handles)
global x;
xneu = 1080*get(handles.slider1,'Value')
x = [0:0.1:xneu/180*pi];
axis(handles.axes1);
plot(sin(x).*x,cos(x).*x,'parent',handles.axes1);
guidata(hObject,handles);
check = get(handles.checkbox1,'Value');
if check == 1
axis(handles.axes2);
plot(x,cos(x).*x,'parent',handles.axes2);
guidata(hObject,handles);
end
% this checkbox is plotting both figures if it is on
function checkbox1_Callback(hObject, eventdata, handles)
pushbutton1_Callback(hObject, eventdata, handles);
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
slider = num2str( round(get(hObject,'Value')*1080,2) );
set(handles.edit1,'String', slider);
pushbutton1_Callback(hObject, eventdata, handles);
function slider1_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function edit1_Callback(hObject, eventdata, handles)
edit = round(str2num( get(hObject,'String') ),2);
set(handles.slider1,'Value',edit/(1080));
pushbutton1_Callback(hObject, eventdata, handles);
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
Kommentare
Kommentar veröffentlichen