MATLAB Second Seminar. Previous lesson Last lesson We learnt how to: Interact with MATLAB in the MATLAB command window by typing commands at the command.

Slides:



Advertisements
Similar presentations
Lecture 5.
Advertisements

Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Introduction to M ATLAB Programming
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
A graphical user interface (GUI) is a pictorial interface to a program. A good GUI can make programs easier to use by providing them with a consistent.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
1 How to create GUI's in MatLAB Rigo Dicochea. 2 Introduction Graphical User Interface (GUI) MatLab provides Graphical User Interface Development Environment(GUIDE)
Introduction to MATLAB
Introduction to MATLAB 3 November Instructor: Andy Newman Office Hours: Stop by room 306 (main building) whenever
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB.
Automating Tasks With Macros. 2 Design a switchboard and dialog box for a graphical user interface Database developers interact directly with Access.
4 Copyright © 2004, Oracle. All rights reserved. Creating a Basic Form Module.
Digital Image Processing Lecture3: Introduction to MATLAB.
What is MATLAB ? MATrix LABratory –Originally, it was a front-end to FORTRAN matrix routines developed in the U. of New Mexico and Stanford –Today.
4. Week 04.March Use of M-File Editor/Debugger: text editor, debugger; editor works with file types in addition to.m (MATLAB “m-files”)
January 24, 2005 Lecture 3 - By P. Lin 1 CPET 190 Lecture 3 Problem Solving with MATLAB
Visual Basic Chapter 1 Mr. Wangler.
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
M ATLAB Tutorial Course 1. Contents Continued  Desktop tools  matrices  Logical &Mathematical operations  Handle Graphics  Ordinary Differential.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
An Introduction to Visual Basic
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Introduction to Matlab & Data Analysis
GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.
Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 6”
Recap Function M-files Syntax of Function M-Files Comments Multiple Input and Output Functions.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
4 Copyright © 2004, Oracle. All rights reserved. Creating a Basic Form Module.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
MATLAB Environment ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
1 M ATLAB Short Course Part (2). 2 Elementary Math Function abs, sign: Absolute value and Signum Function sin, cos, asin, acos…: Triangular functions.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
Matlab 14.html Cost: $100 Available in labs on Windows and Unix machines.
1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Introduction to Matlab & Data Analysis 2015 In this tutorial we will: Build a practical application using GUIDE Learn more about graphical user interface.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
GUI development with Matlab: GUI Front Panel Components GUI development with Matlab: Other GUI Components 1 Other GUI components In this section, we will.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
MATLAB Programming COMM2M Harry R. Erwin, PhD University of Sunderland.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
EGR 115 Introduction to Computing for Engineers
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
Introduction to Matlab Module #10 Page 1 Introduction to Matlab Module #10 – Creating Graphical User Interfaces Topics 1.Overview of GUI Development using.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
MATLAB and SimulinkLecture 61 To days Outline Graphical User Interface (GUI) Exercise on this days topics.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Creating a script create new blank document. Editor options Docking and undocking tabs.
Graphical User Interface in MATLAB
MATLAB Basics Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
Scripts & Functions Scripts and functions are contained in .m-files
Outline Matlab tutorial How to start and exit Matlab Matlab basics.
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
User Defined Functions
StatLab Matlab Workshop
Matlab review Matlab is a numerical analysis system
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Presentation transcript:

MATLAB Second Seminar

Previous lesson Last lesson We learnt how to: Interact with MATLAB in the MATLAB command window by typing commands at the command prompt. Define and use variables. Plot graphs It would be nice if you didn't have to manually type these commands at the command prompt whenever you want to use them.

Write a script that asks for a temperature (in degrees Fahrenheit) computes the equivalent temperature in degrees Celcius. The script should keep running until no number is provided to convert. use isempty Problem

Solution while 1 % use of an infinite loop TinF = input('Temperature in F: '); % get input if isempty(TinF) % how to get out break end TinC = 5*(TinF - 32)/9; % conversion disp(' ') disp([' ==> Temperature in C = ',num2str(TinC)]) disp(' ') end while 1 % use of an infinite loop TinF = input('Temperature in F: '); % get input if isempty(TinF) % how to get out break end TinC = 5*(TinF - 32)/9; % conversion disp(' ') disp([' ==> Temperature in C = ',num2str(TinC)]) disp(' ') end

Functions Functions describe subprograms –Take inputs, generate outputs –Have local variables (invisible in global workspace) Core MATLAB (Built-in) Functions – sin, abs, exp,... Can’t be displayed on screen MATLAB-supplied M-file Functions – mean, linspace, … Ca be displayed on screen User-created M-file Functions

Core MATLAB (Built-in) Functions Elementary built-in functions >> help elfun % a list of these functions Special Math functions >> help specfun Special functions - toolboxes Each toolbox has a list of special functions that you can use sin % Sine. exp % Exponential. abs % Absolute value. round % Round towards nearest integer. sin % Sine. exp % Exponential. abs % Absolute value. round % Round towards nearest integer. lcm % Least common multiple. cart2sph % Transform Cartesian to spherical % coordinates. lcm % Least common multiple. cart2sph % Transform Cartesian to spherical % coordinates.

function y = mean(x) % MEAN Average or mean value. % For vectors, MEAN(x) returns the mean value. % For matrices, MEAN(x) is a row vector % containing the mean value of each column. [m,n] = size(x); if m == 1 m = n; end y = sum(x)/m; function y = mean(x) % MEAN Average or mean value. % For vectors, MEAN(x) returns the mean value. % For matrices, MEAN(x) is a row vector % containing the mean value of each column. [m,n] = size(x); if m == 1 m = n; end y = sum(x)/m; Structure of a Function M-file Keyword: functionFunction Name (same as file name.m) Output Argument(s)Input Argument(s) Online Help MATLAB Code »output_value = mean(input_value) Command Line Syntax

Multiple Input & Output Arguments function r = ourrank(X,tol) % OURRANK Rank of a matrix s = svd(X); if (nargin == 1) tol = max(size(X))*s(1)*eps; end r = sum(s > tol); function r = ourrank(X,tol) % OURRANK Rank of a matrix s = svd(X); if (nargin == 1) tol = max(size(X))*s(1)*eps; end r = sum(s > tol); function [mean,stdev] = ourstat(x) % OURSTAT Mean & std. deviation [m,n] = size(x); if m == 1 m = n; end mean = sum(x)/m; stdev = sqrt(sum(x.^2)/m – mean.^2); function [mean,stdev] = ourstat(x) % OURSTAT Mean & std. deviation [m,n] = size(x); if m == 1 m = n; end mean = sum(x)/m; stdev = sqrt(sum(x.^2)/m – mean.^2); Multiple Input Arguments (, ) Multiple Output Arguments [, ] » RANK = ourrank(rand(5),0.1); » [MEAN,STDEV] = ourstat(1:99);

nargin, nargout, nargchk nargin – number of input arguments - Many of Matlab functions can be run with different number of input variables. nargout – number of output arguments - efficiency nargchk – check if number of input arguments is between some ‘low’ and ‘high’ values

Workspaces in MATLAB MATLAB (or Base) Workspace: – For command line and script file variables. Function Workspaces: – Each function has its own workspace for local variables. – Name of Input/output variables can be either equal or different then the variable name in the calling workspace. – Communicate to Function Workspace via inputs & outputs. Global Workspace: Global variables can be shared by multiple workspaces. (Must be initialized in all relevant workspaces.) Initialize global variables in all relevant workspaces: »global variable_name Initialize global variables in the “source” workspace before referring to them from other workspaces.

Tips for using Global Variables DON’T USE THEM If you absolutely must use them: – Avoid name conflicts >>whos global %shows the contents of the global workspace >>clear global %erases the variable from both local and global workspaces. >>isglobal()

MATLAB Calling Priority High variable built-in function subfunction private function MEX-file P-file M-file Low » cos='This string.'; » cos(8) ans = r » clear cos » cos(8) ans = » cos='This string.'; » cos(8) ans = r » clear cos » cos(8) ans =

Visual Debugging Set Breakpoint Clear Breaks Step In Single Step Continue Quit Debugging Select Workspace Set Auto- Breakpoints

Example: Visual Debugging (2) Editor/Debugger opens the relevant file and identifies the line where the error occurred. Current Location Current Workspace (Function)

Example: Visual Debugging (3) Error message Access to Function’s Workspace Debug Mode

Some Useful MATLAB commands whatList all m-files in current directory dirList all files in current directory lsSame as dir type testDisplay test.m in command window delete testDelete test.m cd a:Change directory to a: chdir a:Same as cd pwdShow current directory which testDisplay current directory path to test.m whyIn case you ever needed a reason

Write a function that asks for a temperature (in degrees Fahrenheit) computes the equivalent temperature in degrees Celcius. The function should give an error massage in case no number is provided to convert. use nargin. Problem

Solution function TinC=temp2(TinF) TinF = input('Temperature in F: '); % get input if nargin==0 % if there is no input disp('no temparture was entered'); TinC=nan; else TinC = 5*(TinF - 32)/9; % conversion disp(' ') disp([' ==> Temperature in C = ',num2str(TinC)]) disp(' ') end function TinC=temp2(TinF) TinF = input('Temperature in F: '); % get input if nargin==0 % if there is no input disp('no temparture was entered'); TinC=nan; else TinC = 5*(TinF - 32)/9; % conversion disp(' ') disp([' ==> Temperature in C = ',num2str(TinC)]) disp(' ') end

MATLAB Input To read files in if the file is an ascii table, use “load” if the file is ascii but not a table, file I/O needs “fopen” and “fclose” Reading in data from file using fopen depends on type of data (binary or text) Default data type is “binary”

What Is GUIDE? Graphical User Interface Design Environment provides a set of tools for creating graphical user interfaces (GUIs). GUIDE automatically generates an M-file that controls how the GUI operates. Starting GUIDE: >> guide OR: Push buttons Static text Pop-up menu axes

GUIDE Tools - The Layout Editor In the Quick Start dialog, select the Blank GUI (Default) template Display names of components: File preferences Show names in component palette Lay out your GUI by dragging components (panels, push buttons, pop-up menus, or axes) from the component palette, into the layout area Drag to resize Component panel Layout area

Using the Property Inspector Labeling the Buttons Select Property Inspector from the View menu. Select button by clicking it. Fill the name in the String field. Activate GUI Property Inspector

Programming a GUI Callbacks are functions that execute in response to some action by the user. A typical action is clicking a push button. You program the GUI by coding one or more callbacks for each of its components. A GUI's callbacks are found in an M-file that GUIDE generates automatically. Callback template for a push button:

Handles structure Save the objects handles: handles.objectname Save global data – can be used outside the function. Example: popup menu: Value – user choice of the popup menu