Sunteți pe pagina 1din 17

Lucrarea nr.

CREAREA INTERFEELOR GRAFICE

Constructia unei interfete grafice cu utilizatorul folosind GUI (Graphical User Interfaces).
Pentru a construi o interfata grafica cu utilizatorul se poate face apel la facilitatile MATLAB
dezvoltate in aceste sens. In cele ce urmeaza vom exemplifica constructia unor interfete grafice
simple de tip GUI folosind MATLAB GUIDE (Graphical User Interfaces Development
Environment).

Pentur crearea interfetei grafice se face apel la MATLAB GUIDE tastand la linia de comanda in
Command Window comanda
>> guide
In cutia de dialog selectati Blank GUI (Default) in campul GUIDE Templates.

Dupa ce ati efectuat selectia clicati pe butonul OK al casutei de dialog. In urma acestei actiuni va
apare o fereastra ce reprezinta mediul de dezvoltare MATLAB GUIDE avand infatisarea ca in
figura de mai jos.

1
Lucrarea nr. 4

In zona din stanga ecranului sunt dispuse obiectele predefinite disponibile in MATLAB.
Fiecare stare a unui obiect este asociat unui identificator unic numit handles, care poate fi folosit
pentru manipularea caracteristicilor obiectului respectiv (caracteristici care sunt numite
proprietile obiectului).

Exemplu 1. Crearea unei interfete pentru adunarea a doua numere, cu urmtoarea structur:

Etapa 1. Crearea aspectului vizual al GUI

2
Lucrarea nr. 4

Sunt necesare urmtoarele componente:


- 2 obiecte de tip Edit Text
- 4 obiecte de tip Static Text
- 1 obiect de tip Push Button
Dup ce ai plasat aceste obiecte trebuie s obinei figura de mai jos. Acum puteti salva interfata
grafica cu numele interfata1 printr-un clic pe meniul File + Save As. In urma operatiunii de
salvare, MATLAB creeaza doua fisiere interfata1.fig si interfata1.m cel de-al doilea fiind in
mod automat deschis.

3
Lucrarea nr. 4

Acest ultim fiier conine codul specific interfeei grafice unde trebuiesc adugate instruciunile
de execuie a anumitor comenzi de interaciune a utilizatorului cu programul.
In continuare se vor schimba proprietile obiectelor plasate pe foaia MATLAB GUIDE. Pentru
aceasta executai un dublu clic pe rnd pe fiecare obiect pentru a deschide fereastra Property
Inspector specific fiecrui obiect grafic. Figura de mai jos reprezint o exemplificare pentru un
obiect de tip Static Text.

4
Lucrarea nr. 4

Etapa 2. Scrierea liniilor de program pentru obiectelor GUI (Callbacks)


Exemplificare pentru un obiect de tip Edit Text
function edit1_Callback(hObject, eventdata, handles)
input=str2double(get(hObject,'String'));
if(isempty(input))
set(hObject,'String','0');
end
guidata(hObject,handles);

Exemplificare pentru obiectul de tip Push Button


function pushbutton1_Callback(hObject, eventdata, handles)
a=get(handles.edit1,'String');
b=get(handles.edit2,'String');
total=str2num(a)+str2num(b);
set(handles.rezultat,'String',num2str(total))
guidata(hObject,handles);

Obs: S-a modificat n Property Inspector proprietatea Tag a obiectului Static Text care conine
rezultatul adunrii n rezultat.

In continuare se salveaz modificarile efectuate in fisierul M si de la linia de comanda a ferestrei


Command Window se tasteaz
>> interfata1
care va lansa interfata grafica creat.

Exemplu 2. Crearea unei interfete pentru reprezentarea grafic, cu urmtoarea structur:


Etapa 1. Crearea aspectului vizual al GUI

5
Lucrarea nr. 4

Sunt necesare urmtoarele componente:


- 2 obiecte de tip Axes
- 3 obiecte de tip Push Button
Dup ce ai plasat aceste obiecte trebuie s obinei figura de mai jos. In urma operatiunii de
salvare, MATLAB creeaza doua fisiere interfata2.fig si interfata2.m cel de-al doilea fiind in
mod automat deschis.

Etapa 2. Scrierea liniilor de program pentru obiectelor GUI (Callbacks)

- n corpul funciei interfata2_OpeningFcn se introduce urmtoarea linie


set(hObject,'toolbar','figure');
pentru adugarea unei bare de comenzi
- Pentru butonul Plot Axes 1
function pushbutton1_Callback(hObject, eventdata, handles)
axes(handles.axes1)
x=0:10;
y=0:10;
plot(x,y)
title('Sistem axe 1');
xlabel('X');
ylabel('Y');
guidata(hObject,handles)

- Pentru butonul Plot Axes 2

6
Lucrarea nr. 4

function pushbutton2_Callback(hObject, eventdata, handles)


axes(handles.axes2)
x=0:10;
y=x.^2;
plot(x,y)
title('Sistem axe 2');
xlabel('X');
ylabel('Y');
guidata(hObject,handles)

- Pentru butonul Sterge grafice


function pushbutton3_Callback(hObject, eventdata, handles)
cla(handles.axes1,'reset')
cla(handles.axes2,'reset')
guidata(hObject,handles)

>> interfata2
care va lansa interfata grafica creat.

Exemplu 3. Crearea unei interfete pentru selectarea mrimii caracterelor.


Etapa 1. Crearea aspectului vizual al GUI
Sunt necesare urmtoarele componente:
- 3 obiecte de tip Static Text
- 1 obiect de tip Popup Menu

7
Lucrarea nr. 4

Executai un dublu clic pe rnd pe obiectul Popup Menu pentru a deschide fereastra Property
Inspector specific acestui obiect grafic.

Se selecteaz String i se introduc urmtoarele:

8
Lucrarea nr. 4

Etapa 2. Scrierea liniilor de program pentru obiectelor GUI (Callbacks)

function popupmenu1_Callback(hObject, eventdata, handles)


switch get(handles.popupmenu1,'Value')
case 1
set(handles.test,'FontSize',8);
case 2
set(handles.test,'FontSize',10);
case 3
set(handles.test,'FontSize',12);
case 4
set(handles.test,'FontSize',14);
case 5
set(handles.test,'FontSize',16);
otherwise
end

Aplicaie: S se construiasc o interfa grafic cu ajutorul creia se calculeaz curentul nominal


al unui motor electric.
Interfaa este format din patru ferestre grafice.
Fereastra 1
- dou obiecte de tip Static Text (scriere pe dou rnduri)
- un obiect de tip Push Button denumit Continua apeleaz Fereastra 2

9
Lucrarea nr. 4

Fereastra 2
- un obiect de tip Static Text
- dou obiecte de tip Radio Button prin selectarea acestora se stabilete tipul de
motor: monofazat sau trifazat
- un obiect de tip Push Button denumit Continua - apeleaz Fereastra 3

Fereastra 3
- cinci obiecte de tip Static Text
- trei obiecte de tip Edit Text se introduc datele necesare

10
Lucrarea nr. 4

- un obiect de tip Push Button denumit Calculeaza - apeleaz Fereastra 4

function fereastra3_OpeningFcn(hObject, eventdata, handles, varargin)


global motor_1
handles.output = hObject;
guidata(hObject, handles);
if motor_1==1
set(handles.text5,'String','Ati ales un motor monofazat')
else
set(handles.text5,'String','Ati ales un motor trifazat')
end

function edit1_Callback(hObject, eventdata, handles)


global pn
pn=str2double(get(hObject,'String'));

global pn cos_fi eta


if isempty(get(handles.edit1,'String')) |
isempty(get(handles.edit2,'String')) ...
| isempty(get(handles.edit3,'String'))
errordlg('Introduceti toate datele')
return
end
if cos_fi>1 | cos_fi<0
errordlg('0 < cos_fi <= 1')
return
end
if eta>1 | eta<0
errordlg('0 < eta < 1')

11
Lucrarea nr. 4

return
end
fereastra4

Fereastra 4
- 11 obiecte de tip Static Text
- dou obiect de tip Push Button

function fereastra4_OpeningFcn(hObject, eventdata, handles, varargin)


global pn cos_fi eta motor_1
if motor_1==1
set(handles.text5,'String','230 V')
crt_nominal=pn*1000/(230*cos_fi*eta);
else
set(handles.text5,'String','400 V')
crt_nominal=pn*1000/(sqrt(3)*400*cos_fi*eta);
end
set(handles.text2,'String',[num2str(crt_nominal),' A'])
set(handles.text7,'String',[num2str(pn),' kW'])
set(handles.text9,'String',num2str(cos_fi))
set(handles.text11,'String',num2str(eta))

Sursa MATLAB fereastra1.m


function varargout = fereastra1(varargin)
% FEREASTRA1 M-file for fereastra1.fig

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...

12
Lucrarea nr. 4

'gui_Singleton', gui_Singleton, ...


'gui_OpeningFcn', @fereastra1_OpeningFcn, ...
'gui_OutputFcn', @fereastra1_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(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
% End initialization code - DO NOT EDIT

% --- Executes just before fereastra1 is made visible.


function fereastra1_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% --- Outputs from this function are returned to the command line.
function varargout = fereastra1_OutputFcn(hObject, eventdata, handles)

% Get default command line output from handles structure


varargout{1} = handles.output;

% --- Executes on button press in pushbutton1.


function pushbutton1_Callback(hObject, eventdata, handles)
pos_size=get(handles.figure1,'Position');
fereastra2
delete(handles.figure1)

Sursa MATLAB fereastra2.m


function varargout = fereastra2(varargin)
% FEREASTRA2 M-file for fereastra2.fig
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @fereastra2_OpeningFcn, ...
'gui_OutputFcn', @fereastra2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(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

13
Lucrarea nr. 4

% End initialization code - DO NOT EDIT


% --- Executes just before fereastra2 is made visible.
function fereastra2_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% --- Outputs from this function are returned to the command line.
function varargout = fereastra2_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;

% --- Executes on button press in radiobutton1 1~


function radiobutton1_Callback(hObject, eventdata, handles)

set(handles.radiobutton1,'Value',1);
set(handles.radiobutton2,'Value',0);

% --- Executes on button press in radiobutton2 3~


function radiobutton2_Callback(hObject, eventdata, handles)
set(handles.radiobutton1,'Value',0);
set(handles.radiobutton2,'Value',1);

% --- Executes on button press in CONTINUA


function pushbutton2_Callback(hObject, eventdata, handles)
global motor_1 motor_3
motor_1=get(handles.radiobutton1,'Value');
motor_3=get(handles.radiobutton2,'Value');

if (motor_1==0 & motor_3==0)


errordlg('Alegeti un tip de motor')
return
end
fereastra3

Sursa MATLAB fereastra3.m


function varargout = fereastra3(varargin)
% FEREASTRA3 M-file for fereastra3.fig
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @fereastra3_OpeningFcn, ...
'gui_OutputFcn', @fereastra3_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(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{:});

14
Lucrarea nr. 4

end
% End initialization code - DO NOT EDIT
% --- Executes just before fereastra3 is made visible.
function fereastra3_OpeningFcn(hObject, eventdata, handles, varargin)
global motor_1
handles.output = hObject;
guidata(hObject, handles);
if motor_1==1
set(handles.text5,'String','Ati ales un motor monofazat')
else
set(handles.text5,'String','Ati ales un motor trifazat')
end

function varargout = fereastra3_OutputFcn(hObject, eventdata, handles)


varargout{1} = handles.output;
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

function edit1_Callback(hObject, eventdata, handles)


global pn
pn=str2double(get(hObject,'String'));

function edit2_CreateFcn(hObject, eventdata, handles)


if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

function edit2_Callback(hObject, eventdata, handles)


global cos_fi
cos_fi=str2double(get(hObject,'String'));

function edit3_CreateFcn(hObject, eventdata, handles)


if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

function edit3_Callback(hObject, eventdata, handles)


global eta
eta=str2double(get(hObject,'String'));

% --- Executes on button press in pushbutton1.


function pushbutton1_Callback(hObject, eventdata, handles)
global pn cos_fi eta
if isempty(get(handles.edit1,'String'))
|isempty(get(handles.edit2,'String')) | isempty(get(handles.edit3,'String'))

15
Lucrarea nr. 4

errordlg('Introduceti toate datele')


return
end
if cos_fi>1 | cos_fi<0
errordlg('0 < cos_fi <= 1')
return
end
if eta>1 | eta<0
errordlg('0 < eta < 1')
return
end

fereastra4

Sursa MATLAB fereastra4.m


function varargout = fereastra4(varargin)
% FEREASTRA4 M-file for fereastra4.fig
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @fereastra4_OpeningFcn, ...
'gui_OutputFcn', @fereastra4_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(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
% End initialization code - DO NOT EDIT
% --- Executes just before fereastra4 is made visible.
function fereastra4_OpeningFcn(hObject, eventdata, handles, varargin)
global pn cos_fi eta motor_1
if motor_1==1
set(handles.text5,'String','230 V')
crt_nominal=pn*1000/(230*cos_fi*eta);
else
set(handles.text5,'String','400 V')
crt_nominal=pn*1000/(sqrt(3)*400*cos_fi*eta);

end
set(handles.text2,'String',[num2str(crt_nominal),' A'])
set(handles.text7,'String',[num2str(pn),' kW'])
set(handles.text9,'String',num2str(cos_fi))
set(handles.text11,'String',num2str(eta))

% Choose default command line output for fereastra4


handles.output = hObject;

% Update handles structure

16
Lucrarea nr. 4

guidata(hObject, handles);

% --- Outputs from this function are returned to the command line.
function varargout = fereastra4_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;

% --- Executes on button press in pushbutton2.


function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fereastra2

% --- Executes on button press in pushbutton3.


function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

close all

17