intro to Matlab GUuuuuuuuuuuuuuuuuI.pptx

AbdoHesham8 6 views 49 slides Aug 16, 2024
Slide 1
Slide 1 of 49
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49

About This Presentation

very good


Slide Content

Introduction to MATLAB GUI Eng. Ahmed Adel Ghoneimy Mechanical Engineering Department Benha Faculty of Engineering Benha university 2023 Presented By:

Graphical User Interface Development Environment (GUIDE) G raphical U ser I nterface (GUI) for a MATLAB program Graphical user interfaces (GUIs ), provide point-and-click control of your software applications, eliminating the need for others to learn a language or type commands in order to run the application . You can share apps both for use within MATLAB and also as standalone desktop or web apps .  G raphical U ser I nterface D evelopment E nvironment (guide) is used to build the GUI.

Graphical User Interface Development Environment (GUIDE) G raphical U ser I nterface (GUI) for a MATLAB program

Graphical User Interface Development Environment (GUIDE) G raphical U ser I nterface (GUI) for a MATLAB program

Graphical User Interface Development Environment (GUIDE) G raphical U ser I nterface (GUI) for a MATLAB program

Graphical User Interface Development Environment (GUIDE)

Graphical User Interface Development Environment (GUIDE) RRR || By Ahmed Adel - 2020

Graphical User Interface Development Environment (GUIDE) To start GUIDE , type guide (no capitals) at the command prompt in the Command window. >> guide

Graphical User Interface Development Environment (GUIDE) The GUIDE window can be resized by grabbing the lower right corner with the mouse and stretching. A GUI is designed by positioning and setting the properties of GUI elements or objects . Most of these GUI objects are instances of a MATLAB class called uicontrol (user interface controls).

Graphical User Interface Development Environment (GUIDE) The GUI objects push button – A button that activates when the user clicks the mouse on it. slider – Enter a real number by adjusting the position of the slide. radio button – Changes its state from unselected to selected and back. checkbox – Changes state from selected (checked) to unselected (unchecked). edit text – Allows the user to input information by typing text into the window. static text – Displays some text; useful for labeling items or reporting the results of a calculation. pop-up menu – Gives the user a list of options from which one may be selected. listbox – Presents a list of options that can be scrolled. Several items can be selected at the same time. toggle button – Pressed once, it stays down until it is pressed again. table – Displays data in a tabular form. axes – A surface on which to display two-dimensional and three-dimensional graphics. panel – Groups controls together visually. button group – Groups a set of radio buttons or toggle buttons so that they act together with the result that only one at a time is selected. activeX control (Windows) – allows the insertion of an ActiveX control made by another program. (Only use these if you really know what you’re doing; improperly configured controls can crash MATLAB.)

Graphical User Interface Development Environment (GUIDE) Arranging various GUI objects by drag and draw as shown various components are inserted in the figure area or GUI area.

Graphical User Interface Development Environment (GUIDE) GUIDE does two things , GUIDE saves all the information you have designed into the GUI, i.e., all the geometric information and all the properties of every GUI object you put in the frame, into a file .fig GUIDE also creates a file .m , stores it in the current MATLAB folder, and loads it into the MATLAB editor. This file contains the program that creates and executes the GUI tool you have made.

Graphical User Interface Push Button BackgroundColor Font Style String Tag Visible Allow the user to interact and execute actions on click Drag and drop the pushbutton and double click on it to show inspector window. Tags The Tag property is very important because it functions as a uniquely specified name for each particular GUI object.

Graphical User Interface edit1 Edit Text BackgroundColor Font Style String Tag Visible field or box that enables user to enter or modify text strings . Use edit text when you want text as input. Drag and drop the Edit Text and double click on it to show inspector window.

Graphical User Interface Static Text Static text displays lines of text. Static text is typically used to: label other controls, provide directions to the user, or indicate values associated with a slider.

Graphical User Interface Simple Program Design and code a simple user-friendly GUI in Matlab to get sum of two inputs

Graphical User Interface GUI – Layout (front end) GUI – Elements Push Button named SUM ــــــــــــــــــــــــ ــــــــــــــــــــــ ــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ Static text named Simple Addition static text named + static text named = static text named result ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ Edit text - empty Edit text - empty

Graphical User Interface GUI – (Back End) On button Click Get value entered by a user from edit text 1 Get value entered by a user from edit text 2 Calculate the sum of two values Set or Print sum value to static text How To get and set ?!

Graphical User Interface The handle to a GUI object is a coded version of its address in the computer’s memory. It is used to communicate with the object , principally through the get and set commands. Inside a callback function the handles of all the objects in the tool are available through the MATLAB structure handles (of class struct ). GUIDE creates the fields of the handles structure to be the same as the Tags of the GUI objects. For example, the handle for an object with Tag first is handles . first handles

Graphical User Interface set( handles .tag , ' property ', value); get( handles .tag , ‘ property' ); get and set

Graphical User Interface font_value = get( handles .first , ' FontSize ' ); Get To ask an object to report the value of a specific property, use the get command and the handles structure. For example, to retrieve the value of the FontSize property of the textbox with Tag first, the command: returns the numerical value of the font size and stores it in the variable font_value

Graphical User Interface Value = 14; set( handles .tag , ' FontSize ', value); Set set To tell an object to change the Value of a specific property, use the set command and the handles structure . For example, to change the ‘string’ value of textbox with Tag ‘first’ to the value stored in the variable value , use the following command :

Graphical User Interface How To get value from edit text when click on button ? Callback function A callback is  a function that executes when a user interacts with a UI component GUIDE constructs the name of the callback function associated with each object using the Tag you assigned to the object. For example, if the object has the Tag “first,” its callback function is named first_Callback .

Graphical User Interface function first_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) first=get(handles.edit1, 'String ' ); % get data from first edit text with tag edit1 first=str2double(first ); %convert string value to double second=get(handles.edit2, 'String ' ); % get data from first edit text with tag edit2 second=str2double(second); % convert string value to double sum=first + second; %sum of two values set(handles.text4, 'String' , sum); %set sum value on static text with tag text4

Graphical User Interface

Symbolic Computations in MATLAB Symbolic Math Toolbox™ enables you to perform symbolic computations . Functions are called using the familiar MATLAB syntax and are available for integration , differentiation , simplification , equation solving , and other mathematical tasks. Symbolic computations augment the numeric capabilities of MATLAB. You can convert symbolic results to numeric results and use them outside Symbolic Math Toolbox

Symbolic Computations in MATLAB syms a b c; % define symbolic variables A = sym (' A ', [3 4]) % define symbolic Matrix variables

Symbolic Computations in MATLAB Subs Symbolic substitution double Convert symbolic values to MATLAB double precision Multi Subs.. syms a b ; y = a + b; y = subs(y,{ a,b },{4,3}); y = double(y );

Symbolic Computations in MATLAB Differentiation syms x ; y = 3*x^3 + sin(x^2); y = diff( y,x ); Differentiate this expression with respect to the variable  x   syms x t ; y = 3*x^3*t + sin(x^2*t); y = diff( y,x,t ) y = simplify(y) Differentiate this expression with respect to the variables  x, t  

Symbolic Computations in MATLAB Integration syms x f = -2*x/(1+x^2)^2; f_int = int( f,x ); % indefinite integrals f_int = int(f,0,1); % Definite Integration with respect to the variable x Definite and indefinite integrals   fun = @( x,y ) 1./( sqrt (x + y) .* (1 + x + y).^2 ) q = integral2 (fun,0,1,0,1) Double integral Integration with respect to the variable  x   and y

Graphical User Interface Axes An axes object displays graphical information, such as plots ( two-dimensional and three-dimensional) and images . title ( hObject , 'Your title text' ) xlabel ( hObject , 'X' ); ylabel ( hObject , 'y' ); zlabel ( hObject , 'z' ) grid ( hObject , 'on ' ) hold (handles.axes1, 'on ' ) cla (handles.axes1 ) ; What is difference between hObject and handles.axes1 ?

Graphical User Interface Check Box A check box is a UI component for indicating the state of a preference or option checked= get (handles.checkbox1, 'value' ); To get state (value) of check box with tag checkbox1

Graphical User Interface Design The following GUI and develop it to plot checked sine and/or cosine as the user choose

Graphical User Interface Design The following GUI and develop it to plot checked sine and/or cosine as the user choose function pushbutton1_Callback( hObject , eventdata , handles ) cla (handles.axes1); % clear axes1 hold(handles.axes1, 'on ' ); % hold on title(handles.axes1, 'Plot of sine and cosine waves ' ) % title; xlabel (handles.axes1, 'X ' ); % xlabel ; ylabel (handles.axes1, 'y ' ); % ylabel ; zlabel (handles.axes1, 'z ' ) % zlabel ; grid(handles.axes1, 'on ' ) % show grid; checked=get(handles.checkbox1, 'value ' ); % get value of check box1; if checked % use if to check if checkbox1 is checked; x = 0:1:360; y = sind (x); plot(handles.axes1,x,y , 'r' , 'linewidth' ,2 ) % plot sine wave; end checked2=get(handles.checkbox2, 'value' ); % get value of check box2; if checked2 % use if to check if checkbox2 is checked ; x = 0:1:360; y = cosd (x); plot(handles.axes1,x,y , 'b' , 'linewidth' ,2 ) % plot cosine wave; end

Graphical User Interface Display a photo using axes function pushbutton1_Callback( hObject , eventdata , handles) img = imread ( 'car.png' ); axes(handles.axes1) imshow ( img );

Graphical User Interface Bezier Curve – GUI

Graphical User Interface Bezier Curve – GUI Control points A (4 , 8, 4) , B (7 , 9, 6) , C (8 , 1, 0) , D (10 , 4, 7) which define the control polygon

Graphical User Interface Bezier Curve – GUI Control points A (4 , 8, 4) , B (7 , 9, 6) , C (8 , 1, 0) , D (10 , 4, 7) which define the control polygon syms u ; %define symbolic variable u x=[4 7 8 10]; y=[8 9 1 4]; z=[4 6 0 7]; P =[ x;y;z ]; % control points matrix n = length(x)-1 ; % control points matrix B=0; for i = 0:n B=B + factorial(n)/(factorial(i)*factorial(n-i))* ... u^i * (1-u)^(n-i) *P(:,i+1); end i=0; for U=0:0.01:1 i=i+1; C_pnts (:,i)=double(subs( B,u,U )); End

Graphical User Interface str2num() Convert character array or string to numeric array

Graphical User Interface str2num() Convert character array or string to numeric array Try to get vector input as shown x=get(handles.edit1, 'string ' ) x = str2num(x ); ــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ If str2num() doesn’t work try the following: x = str2double( strsplit (x {:},’,’) );

Graphical User Interface Bezier Curve – GUI cla (handles.axes1 ); xlabel (handles.axes1, 'X' ); ylabel (handles.axes1, 'y' ); zlabel (handles.axes1, 'z' ) x=get(handles.edit1, 'string' ); x = str2num(x ); x= str2double( strsplit (x{:}, ’,’ )); y=get(handles.edit2, 'string' ); y = str2num(y); z=get(handles.edit3, 'string' ); z = str2num(z); syms u ; P =[ x;y;z ]; B=0 ; n = length(x)-1; for i = 0:n B=B+ factorial(n)/(factorial(i)*factorial(n-i))* ... u^i * (1-u)^(n-i) *P(:,i+1); End i=0; for U=0:0.01:1 i=i+1; C_pnts (:, i)= double(subs( B,u,U )); End plot3(handles.axes1 , C_pnts (1,:), C_pnts (2,:), C_pnts (3 ,:), 'r' ); hold(handles.axes1 , 'on' ) grid(handles.axes1, 'on' ) checked=get(handles.checkbox1, 'value' ); if checked plot3(handles.axes1,x,y,z, 'b' , 'linewidth' ,2); end

Graphical User Interface Bezier Curve – GUI Control points A (4 , 8, 4) , B (7 , 9, 6) , C (8 , 1, 0) , D (10 , 4, 7) which define the control polygon

Graphical User Interface Slider Is used to get value from min to max when sliding BackgroundColor Font Style String Tag Visible Current value Max - value Min - value Set maximum - value via Max property Set minimum - value via Min property

Graphical User Interface Simple GUI using a slider Design and develop the following GUI to read the value from the slider and show it in edit text as shown function slider1_Callback ( hObject , eventdata , handles) value = get( hObject , 'value' ); set(handles.edit1 , 'string' ,value);

Graphical User Interface Message dialog box

Graphical User Interface Patch Plot x = [0 1 1 0]; y = [0 0 1 1]; patch( x,y, 'r ' )

Graphical User Interface Multi-faces using Patch points = [0 0 0; 0 1 0; 1 1 0; 1 0 0; 0 0 1; 0 1 1; 1 1 1; 1 0 1 ]; faces = [ 1 2 3 4 ; 2 6 7 3; 4 3 7 8 ; 1 5 8 4; 1 2 6 5 ; 5 6 7 8]; patch( 'Vertices ' , points , 'Faces' ,faces, 'FaceColor' , 'r ' , 'facealpha ' ,0.7)

Graphical User Interface Task: Design and develop a simple Matlab GUI to apply transformations on a unit cube Design GUI as shown or make your own design