0% found this document useful (0 votes)
15 views15 pages

CAG2425 Lab 11

Uploaded by

paulgiurgi04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views15 pages

CAG2425 Lab 11

Uploaded by

paulgiurgi04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Matlab Laboratory 11.

Graphical User Interfaces – Part3


Aim of the laboratory

 Matlab gui-s obtained programmatically


Necessary equipment

 Workstations with LTSpice and MATLAB

*Verify
Example1: Build an interface that does the following:
- represents graphically functions sin, cos and sin+cos in 3 different windows
- allows modifying the amplitudes for sin and cos, frequency and number of
displayed periods
- the variable for sine amplitude is A, for cos amplitude is B, for frequency is f and
number of periods is N
- The initial values are A=10; B=5; f=50 Hz; N=4
- The gui contains a button for CLOSE and a button for RESET (resetting the
characteristics to their initial values)
- The interface should also contain a menu for: displaying the front page, resetting the
characteristics to the initial values, displaying a jpg image and a closing function

The trigonometric functions are defined as:


f1=A*sin(2*pi*f*t); % first function,sine with amplitude A
f2=B*cos(5*2*pi*f*t); % second function,cos with amplitude B
f3=f1+f2; % their sum

%
% main.m file
%
close all;
A=10; % magnitude of signal 1 = sinus
B=5; % magnitude signal 2 = cosinus
f=50; % defining the frequency in Hz
N=4; % number of displayed cycles
%creating a new figure
Fig=figure('Name','Laboratory 11',...
'Units','normalized',...
'NumberTitle','off',...
'Position',[0.1 0.1 0.8 0.8],...
'Color',[0.5 0.5 0.9]);

% creates user menu item in main menu


meniu=uimenu(Fig,'Text','User Menu');
% menu item that opens front page
item1=uimenu(meniu,"Text",'DOCX Front Page', ...
'Accelerator','A',...
'Callback', 'open("FrontPage.docx")' );
% menu item that resets characteristics to initial values
item2=uimenu(meniu,"Text",'Reset to initial', ...
'Accelerator','B',...
'Callback', 'interfata(10,5,50,4);' );
%displays a picture using a function
item3=uimenu(meniu,"Text",'Display Picture', ...
'Accelerator','C',...
'Callback','display_pic');
% menu item for close button
item4=uimenu(meniu,"Text",'CLOSE', ...
'Separator','on',...
'Accelerator','X',...
'Callback','close');

% pushbuton CLOSE
uicontrol('Style','pushbutton',...
'Units','normalized',...
'Position',[0.9 0.9 0.08 .05],...
'String','CLOSE',...
'Backgroundcolor','#FAF0E6',...
'ForegroundColor','#800080',...
'FontName','Arial',...
'FontWeight','bold',...
'FontSize',12,...
'Callback','close');

% button indicating default values


uicontrol('Style','text',...
'Units','normalized',...
'Position',[0.5 0.83 0.37 .08],...
'Backgroundcolor',[0.5 0.5 0.9],...
'FontName','Arial',...
'FontWeight','bold',...
'FontSize',18,...
'String','Default values: A=10, B=5, f=50, N=4');

%
% function file interfata
%
function interfata(A,B,f,N)

T=1/f; % defining the cycle of the signal


t=0:T/100:N*T; % representation range in time
x1=A*sin(2*pi*f*t); % first function
x2=B*cos(5*2*pi*f*t); % second function
x3=x1+x2; % sum of the two functions sine and cosine

% Text Button for variable A = sine Amplitude


uicontrol('Style','text',...
'Units','normalized',...
'Position',[0.9 0.83 0.08 .05],...
'Backgroundcolor',[0.5 0.5 0.9],...
'String','A');

% Edit Button for A


uicontrol('Style','edit',...
'Units','normalized',...
'Position',[0.9 0.80 0.08 .05],...
'String',A,...
'Backgroundcolor','#FAF0E6',...
'Callback','A=str2num(get(gco,"String"));interfata(A,B,f,N);');

% Text Button for variable B = cosine Amplitude


uicontrol('Style','text',...
'Units','normalized',...
'Position',[0.9 0.73 0.08 .05],...
'backgroundcolor',[0.5 0.5 0.9],...
'string','B');

% Edit Button for B


uicontrol('Style','edit',...
'Units','normalized',...
'Position',[0.9 0.70 0.08 .05],...
'Backgroundcolor','#FAF0E6',...
'String',B,...
'Callback','B=str2num(get(gco,"String"));interfata(A,B,f,N);');

%Text Button for f =frequency


uicontrol('Style','text',...
'Units','normalized',...
'Position',[0.9 0.63 0.08 .05],...
'Backgroundcolor',[0.5 0.5 0.9],...
'String','f');

% Edit Button for f


uicontrol('Style','edit',...
'Units','normalized',...
'Position',[0.9 0.60 0.08 .05],...
'Backgroundcolor','#FAF0E6',...
'String',f,...
'Callback','f=str2num(get(gco,"String"));interfata(A,B,f,N);');

% Text Button for N =nr of cycles to be displayed


uicontrol('Style','text',...
'Units','normalized',...
'Position',[0.9 0.53 0.08 .05],...
'Backgroundcolor',[0.5 0.5 0.9],...
'String','N');

% Edit Button for N


uicontrol('Style','edit',...
'Units','normalized',...
'Position',[0.9 0.50 0.08 .05],...
'Backgroundcolor','#FAF0E6',...
'String',N,...
'Callback','N=str2num(get(gco,"String"));interfata(A,B,f,N);');

% Pushbutton for Reset - returns the characteristics


uicontrol('Style','pushbutton',...
'Units','normalized',...
'Position',[0.85 0.2 0.13 .05],...
'String','Reset to Initial Values',...
'Backgroundcolor','#FAF0E6',...
'ForegroundColor','#800080',...
'FontName','Arial',...
'FontWeight','bold',...
'FontSize',12,...
'Callback','interfata(10,5,50,4);'); %call with initial values

%setting the plot windows


subplot('position',[0.1 0.72 0.4 0.25]);
plot(t,x1,'m', 'LineWidth',1.5);
grid on;
title('Sinus ');
xlabel('time [s]');
ylabel('Amplitude A [V]');

subplot('position',[0.1 0.38 0.4 0.25]);


plot(t,x2, 'm','LineWidth',1.5);
grid on;
title('Cosinus ');
xlabel('time [s]');
ylabel('Amplitude B [V]');

subplot('position',[0.1 0.05 0.4 0.25]);


plot(t,x3,'b', 'LineWidth',1.5);
grid on;
title('Sum of Sin+Cos ');
xlabel('time [s]');
ylabel('Amplitude A+B [V]');
end

%
% function file display_pic
%
function display_pic
ax=axes('Position',[0.58 0.5 0.25 0.25]);
imshow('sincos.jpg','Parent',ax )
end
Example2: We present a gui example for the inverting and noninverting amplifier. Consider
a main window having the:
 Title Amplifier
 3 buttons in the center for:
o Documentation
o Noninverting amplifier
o Inverting amplifiers
o And a Close button on the right down part
The button for the Documentation will open a .pdf, .docx or .html document containing the
theoretical part of the project like title, author, theme, introduction, formulas, calculus, obtained
results, graphics, references, appendixes and whatever you feel the need to add.
The button for Inverting and the button for Noninverting amplifier circuits create a new
figure in each case, but the figure has the same structure and layout.
The two figures contain:
 A title
 A panel for parameter buttons – name and their values
 A panel for chosing the signal to be plotted: sinusoidal or triangular
 A figure showing the circuit
 A part of the figure window containing the plots for the input and output voltages in
respect to time but also the voltage input-output characteristics.
These windows have also a „Return” button, for returning to the main menu of the GUI.
A menu can be added in the menu bar of the figure or anything else you need to develop and
optimise in your application.

% main file mySTART.m


close all;
% initial settings for circuit variables
A=5;
R1=20e3; R2=10e3;
Vps1=12; Vps2=-12;
f=50e2;
N=5;
d=1;

%creating a new figure for the main interface


figure ('Name','Amplifiers',...
'Numbertitle', 'off',...
'Color','#CCCCFF',...
'Units','normalized',...
'Position',[0.3 0.3 0.3 0.5]);
% TITLE
uicontrol('Style','text',...
'Units','normalized',...
'Fontsize', 24,...
'FontWeight','bold',...
'BackgroundColor','#CCCCFF',...
'Position',[0.35 0.8 0.34 0.09],...
'String','AMPLIFIERS');

% button to display the documentation


uicontrol('Style','pushbutton',...
'Units','normalized',...
'Fontsize', 14,...
'BackgroundColor','#D6CADD',...
'Position',[0.3 0.6 0.45 0.1],...
'String','Documentation',...
'Callback','documentation');

% close button
uicontrol('Style','pushbutton',...
'Units','normalized',...
'FontWeight','bold',...
'Fontsize', 12,...
'BackgroundColor','#D6CADD',...
'Position',[0.85 0.03 0.1 0.06],...
'string','Close',...
'Callback','close');

% creates the figure for the noninverting amplifier


uicontrol('Style','pushbutton',...
'Units','normalized',...
'Fontsize', 14,...
'BackgroundColor','#D6CADD',...
'Position',[0.3 0.4 0.45 0.1],...
'String','NONINVERTING Amplifier',...
'Callback','avin=0;createfig(avin,d,A,R1,R2,Vps1,Vps2,f,N)');
% the variable avin is set to 0
% and the "createfig" function is called

% creates the figure for the inverting amplifier


uicontrol('Style','pushbutton',...
'Units','normalized',...
'Fontsize', 14,...
'BackgroundColor','#D6CADD',...
'Position',[0.3 0.2 0.45 0.1],...
'String','INVERTING Amplifier',...
'Callback','avin=1;createfig(avin,d,A,R1,R2,Vps1,Vps2,f,N)');
% the variable avin is set to 1 and "createfig" function
% is called

%
% function file createfig.m
%
function createfig(avin,d,A,R1,R2,Vps1,Vps2,f,N)
% if the user pushed the "Noninverting Amp"
% button, the title of the figure
% becomes "Noninverting Amplifiers"
% and variable avin=0
if avin==0
figurename="NonInverting Amplifiers";
else

% if the user pushed the "Inverting Amp"


% button, the title of the figure
% becomes "Inverting Amplifiers"
% and variable avin=1
figurename="Inverting Amplifiers";
end
% a new figure is created for the circuit
% the name of the figure is variable
% depending on the button the user pushed
fig=figure('Name',figurename,...
'Units','normalized',...
'NumberTitle','off',...
'Position',[0.05 0.1 0.9 0.8],...
'Color','#CCCCFF');

% if avin=0 the user pushed the "Noninverting Amp"


% button and the image with the inverting
% amplifier circuit is displayed
if avin==0
w=imread('noninverting1.JPG');
else

% if avin=1 the user pushed the "Inverting Amp"


% button and the image with the inverting
% amplifier circuit is displayed
w=imread('inverting1.JPG');
end

% w is the handle of the circuit image


imagesc(w);
axis off;
% sets the position of the current axes
% in the figure with handle fig
set(gca,'Position',[0.03 0.03 0.45 0.45]);

%pushbutton for closing the circuit


% window with the handle fig
uicontrol('Style','pushbutton',...
'Units','normalized',...
'Position',[0.305 0.5 0.15 0.06],...
'BackgroundColor', '#CCCCFF',...
'String','Return',...
'FontSize',16,...
'Callback','close');

% call of the interface containing the parameters


% their values, the signal representations
% and the menu for chosing a sinus or triangular signal
myInterface(avin,d,A,R1,R2,Vps1,Vps2,f,N)
% end of „createfig” function and file
end

%
% function file documentation.m
% opens the documentation file
% here the "Documentation.docx" file
% documentation can be in format
% .pdf, .html, .docx etc
function documentation
open('Documentation.docx');
% end of the documentation.m file
end

%
% function file myInterface.m
%
function myInterface(avin,d,A,R1,R2,Vps1,Vps2,f,N)
% creates a container for the buttons
% of the wave form types=SINUS OR TRIANGLE
Tip_semnal=uibuttongroup('Visible','on',...
'BackgroundColor','#D6CADD',...%[1 1 0.8],...
'ForegroundColor','black',...
'Title','Waveform',...
'FontSize',14,...
'TitlePosition','centertop',...
'Position',[ 0.3 0.6 0.16 0.37]);

% if the user pushes the button "Sinus"


% d=1 and the input voltage is a sine wave
% observe in callback
uicontrol('Style','pushbutton',...
'Units','normalized',...
'Position',[0.1 0.6 0.8 0.2],...
'Fontsize', 14,...
'String','SINUS',...
'backgroundcolor', [0.9 0.9 0.9],...
'Parent', Tip_semnal,...
'Callback', 'd=1;myInterface(avin,d,A,R1,R2,Vps1,Vps2,f,N)');

% if the user pushes the button "Triangle"


% d=2 and the input voltage is a triangular wave
% observe in callback
uicontrol('Style','pushbutton',...
'Units','normalized',...
'Position',[0.1 0.2 0.8 0.2],...
'Fontsize', 14,...
'String','TRIANGLE',...
'backgroundcolor', [0.9 0.9 0.9],...
'Parent', Tip_semnal,...
'Callback', 'd=2;myInterface(avin,d,A,R1,R2,Vps1,Vps2,f,N)');

% the button container group is created


% with the circuit parameter names textbuttons
% and edit buttons with circuit parameter values
Parameters=uibuttongroup('Visible','on',...
'BackgroundColor','#D6CADD',...
'ForegroundColor','black',...
'Title','Parameters',...
'FontSize',14,...
'TitlePosition','centertop',...
'Position', [ 0.05 0.6 0.18 0.37]);
% text button for R1
uicontrol('Style','text',...
'Units','normalized',...
'Position', [0.1 0.76 0.27 0.1],...
'backgroundcolor', [0.9 0.9 0.9],...
'foregroundcolor','black',...
'fontweight', 'bold',...
'fontsize', 10,...
'string','R1',...
'Parent',Parameters);
% edit for R1
uicontrol('Style','edit',...
'Units','normalized',...
'Position',[0.5 0.76 0.27 0.1],...
'backgroundcolor',[0.9 0.9 0.9],...
'foregroundcolor','black',...
'fontweight','bold',...
'fontsize',10,...
'String',R1,...
'Parent',Parameters,...
'Callback',['R1=str2num(get(gco, "String")),' ...
'myInterface(avin,d,A,R1,R2,Vps1,Vps2,f,N)']);
% text button for R2
uicontrol('Style','text',...
'Units','normalized',...
'Position',[0.1 0.65 0.27 0.1],...
'backgroundcolor',[0.9 0.9 0.9] ,...
'foregroundcolor','black',...
'fontweight','bold',...
'fontsize',10,...
'string','R2',...
'Parent',Parameters);
% edit for R2
uicontrol('Style','edit',...
'Units','normalized',...
'Position',[0.5 0.65 0.27 0.1],...
'backgroundcolor',[0.9 0.9 0.9],...
'foregroundcolor','black',...
'fontweight','bold',...
'fontsize',10,...
'String',R2,...
'Parent',Parameters,..
'Callback',['R2=str2num(get(gco, "String")),' ...
'myInterface(avin,d,A,R1,R2,Vps1,Vps2,f,N)']);
% text button for +Vps
uicontrol('Style','text',...
'Units','normalized',...
'Position',[0.1 0.54 0.27 0.1],...
'backgroundcolor',[0.9 0.9 0.9] ,...
'foregroundcolor','black',...
'fontweight','bold',...
'fontsize',10,...
'string','+Vps',...
'Parent',Parameters);
% edit for +Vps
uicontrol('Style','edit',...
'Units','normalized',...
'Position',[0.5 0.54 0.27 0.1],...
'backgroundcolor',[0.9 0.9 0.9],...
'foregroundcolor','black',...
'fontweight','bold',...
'fontsize',10,...
'String',Vps1,... % Vps1 = +Vps
'Parent',Parameters,...
'Callback',['Vps1=str2num(get(gco,"String")),' ...
'myInterface(avin,d,A,R1,R2,Vps1,Vps2,f,N)']);
% text button for -Vps
uicontrol('Style','text',...
'Units','normalized',...
'Position',[0.1 0.43 0.27 0.1],...
'backgroundcolor',[0.9 0.9 0.9] ,...
'foregroundcolor','black',...
'fontweight','bold',...
'fontsize',10,...
'string','-Vps',...
'Parent',Parameters);
% edit for -Vps
uicontrol('Style','edit',...
'Units','normalized',...
'Position',[0.5 0.43 0.27 0.1],...
'backgroundcolor',[0.9 0.9 0.9],...
'foregroundcolor','black',...
'fontweight','bold',...
'fontsize',10,...
'String',Vps2,...
'Parent',Parameters,...
'Callback',['Vps2=str2num(get(gco,"String")),' ...
'myInterface(avin,d,A,R1,R2,Vps1,Vps2,f,N)']);
% text button for f
uicontrol('Style','text',...
'Units','normalized',...
'Position',[0.1 0.32 0.27 0.1],...
'backgroundcolor',[0.9 0.9 0.9] ,...
'foregroundcolor','black',...
'fontweight','bold',...
'fontsize',10,...
'string','f',...
'Parent',Parameters);
% edit for f
uicontrol('Style','edit',...
'Units','normalized',...
'Position',[0.5 0.32 0.27 0.1],...
'backgroundcolor',[0.9 0.9 0.9],...
'foregroundcolor','black',...
'fontweight','bold',...
'fontsize',10,...
'String',f,...
'Parent',Parameters,...
'Callback',['f=str2num(get(gco,"String")),' ...
'myInterface(avin,d,A,R1,R2,Vps1,Vps2,f,N)']);
% text button for A
uicontrol('Style','text',...
'Units','normalized',...
'Position',[0.1 0.21 0.27 0.1],...
'backgroundcolor',[0.9 0.9 0.9] ,...
'foregroundcolor','black',...
'fontweight','bold',...
'fontsize',10,...
'String','A',...
'Parent',Parameters);
% edit for A
uicontrol('Style','edit',...
'Units','normalized',...
'Position',[0.5 0.21 0.27 0.1],...
'backgroundcolor',[0.9 0.9 0.9],...
'foregroundcolor','black',...
'fontweight','bold',...
'fontsize',10,...
'String',A,...
'Parent',Parameters,...
'Callback',['A=str2num(get(gco, "String")),' ...
'myInterface(avin,d,A,R1,R2,Vps1,Vps2,f,N)']);
% text button for N
uicontrol('Style','text',...
'Units','normalized',...
'Position',[0.1 0.1 0.27 0.1],...
'backgroundcolor',[0.9 0.9 0.9] ,...
'foregroundcolor','black',...
'fontweight','bold',...
'fontsize',10,...
'String','N',...
'Parent',Parameters);
% edit for N
uicontrol('Style','edit',...
'Units','normalized',...
'Position',[0.5 0.1 0.27 0.1],...
'backgroundcolor',[0.9 0.9 0.9],...
'foregroundcolor','black',...
'fontweight','bold',...
'fontsize',10,...
'String',N,...
'Parent',Parameters,...
'Callback',['N=str2num(get(gco, "String")),' ...
'myInterface(avin,d,A,R1,R2,Vps1,Vps2,f,N)']);

% calculates the gain for each amplifier


if avin==0
%voltage gain for noninverting amp
Av=1+R2./R1;
else
%voltage gain for inverting amp
Av=-(R2./R1);
end
%period
T=1/f;
%time range
t=0:T/1000:N*T;
% Calculates the input and output voltages
% for each type of amplifier
if d==1 % d==1 sine wave
Vi=A*sin(2*pi*f*t);
Vo=Av*A*sin(2*pi*f*t);
else % d=2 triangular wave
Vi=(2.*A./pi).*asin(sin(2*pi*f*t));
Vo=Av*(2.*A/pi).*asin(sin(2*pi*f*t));
end
%limitations for the current
k=max(size(t));
for i=1:k
if(Vo(i)>Vps1) % Vps1 = +Vps
Vo(i)=Vps1;
end
if(Vo(i)<Vps2) % Vps2 = -Vps
Vo(i)=Vps2;
end
end
clear k;

% plot for Vi= input voltage


subplot('position',[0.55 0.72 0.4 0.25]);
plot(t,Vi,'-.k');
grid on;
title('Input Voltage');
xlabel('time [s]');
ylabel('Amplitude [V]');
legend('Vi');

% plot for Vo= output voltage


subplot('position',[0.55 0.38 0.4 0.25]);
plot(t,Vo,'--b');
grid on;
title('Output Voltage');
xlabel('time [s]');
ylabel('Amplitude [V]');
legend('Vo');

% plot for x=Vi, y=Vo


subplot('position',[0.55 0.05 0.4 0.25]);
plot(Vi,Vo,'-r');
grid on;
title('VTC');
xlabel('Input Voltage [V]');
ylabel('Output Voltage [V]');
legend('Vo/Vi');
% end of the file myInterface.m
end

You might also like