Quick MATLAB Syntax Guide

Similar documents
Matlab Quick Command Reference Sheet: Page 1

Programming in Mathematics. Mili I. Shah

Chapter 1 Introduction to MATLAB

Introduction to MATLAB

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

Introduction to MATLAB. Computational Probability and Statistics CIS 2033 Section 003

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

A very brief Matlab introduction

Consider this m file that creates a file that you can load data into called rain.txt

Introduction to Matlab

Introduction to PartSim and Matlab

To start using Matlab, you only need be concerned with the command window for now.

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Introduction to Engineering gii

Chapter 2. MATLAB Basis

ENGR 1181c Midterm Exam 2: Study Guide and Practice Problems

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations

ANSI C Programming Simple Programs

Introduction to MATLAB for Numerical Analysis and Mathematical Modeling. Selis Önel, PhD

MATLAB. MATLAB Review. MATLAB Basics: Variables. MATLAB Basics: Variables. MATLAB Basics: Subarrays. MATLAB Basics: Subarrays

ENGR 1181 Autumn 2015 Final Exam Study Guide and Practice Problems

Introduction to Matlab

PART 1 PROGRAMMING WITH MATHLAB

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

Introduction to MATLAB Practical 1

Prof. Manoochehr Shirzaei. RaTlab.asu.edu

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

What is a Function? EF102 - Spring, A&S Lecture 4 Matlab Functions

A QUICK INTRODUCTION TO MATLAB

Introduction to MATLAB

MATLAB Vocabulary. Gerald Recktenwald. Version 0.965, 25 February 2017

A QUICK INTRODUCTION TO MATLAB. Intro to matlab getting started

Introduction to MatLab. Introduction to MatLab K. Craig 1

What is Matlab? The command line Variables Operators Functions

Introduction to MATLAB

Introduction to Matlab

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

CT 229 Java Syntax Continued

Finding, Starting and Using Matlab

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

Lecturer: Keyvan Dehmamy

Using the fprintf command to save output to a file.

Basic MATLAB Tutorial

Methods CSC 121 Fall 2014 Howard Rosenthal

Programming in MATLAB

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Chapter 4: Basic C Operators

LAB 1 General MATLAB Information 1

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

ENGR 1181 Midterm Exam 2: Study Guide and Practice Problems

Welcome to EGR 106 Foundations of Engineering II

Engineering Problem Solving with C++, Etter/Ingber

Matlab Programming Introduction 1 2

9 Using Equation Networks

Chapter 2. Outline. Simple C++ Programs

STAT 391 Handout 1 Making Plots with Matlab Mar 26, 2006

Introduction to Computer Programming with MATLAB Matlab Fundamentals. Selis Önel, PhD

Chapter 2 (Part 2) MATLAB Basics. dr.dcd.h CS 101 /SJC 5th Edition 1

Introduction to MATLAB Programming. Chapter 3. Linguaggio Programmazione Matlab-Simulink (2017/2018)

Process Optimization

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Introduction to Programming

Matlab programming, plotting and data handling

CSE 123. Lecture 9. Formatted. Input/Output Operations

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

MATLAB QUICK START TUTORIAL

Digital Image Analysis and Processing CPE

Maths Functions User Manual

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

EP375 Computational Physics

1 Introduction to Matlab

MATLAB Quick Reference

Eng Marine Production Management. Introduction to Matlab

What is MATLAB? It is a high-level programming language. for numerical computations for symbolic computations for scientific visualizations

Introduction to Python, Cplex and Gurobi

Goals for This Lecture:

Importing and Exporting Data

Introduction to MATLAB

Introduction to Matlab

Programming 1. Script files. help cd Example:

This is a basic tutorial for the MATLAB program which is a high-performance language for technical computing for platforms:

A Quick Tutorial on MATLAB. Zeeshan Ali

An Introductory Tutorial on Matlab

MATLAB. A Tutorial By. Masood Ejaz

Introduction to GNU-Octave

Matlab Programming Arrays and Scripts 1 2

INTRODUCTION TO MATLAB

1. Register an account on: using your Oxford address

workspace list the variables and describe their matrix sizes 4x1 matrix (4 rows, 1 column) x=[3.4, 7, 2.2] 1x3 matrix (1 row, 3 columns)

EE 301 Signals & Systems I MATLAB Tutorial with Questions

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

General MATLAB Information 1

Custom Variables (Virtual Variables)

AN INTRODUCTION TO MATLAB

MATLAB Introduction to MATLAB Programming

Transcription:

Quick MATLAB Syntax Guide Some useful things, not everything if-statement Structure: if (a<0) % general structure of if-statement. logical comparators. x = 1 %calculation when if true < > <= >= = = ~= % closing statement of if statement if-elseif-else Statment Structure:. logical operators. if (a<0) % general structure of if-statement ~ NOT x=1 % calculation for if true & AND elseif(a>0) % elseif general statement OR x=2 % calculation for elseif true else % else statement x=3 % calculation when no if or elseif is true % closing statement of if statement while Loop Structure: while (k>0) % enter loop when while statement is true k=k-1 % calculation when while is true % closing statement of while statement for Loop Structure: for k=1:0.001:10 % enter for loop, where k =first:increment:last x=x+5 % calculation if (k>7) % option method to exit loop before the of the for loop break % s program to statement following %for %closing statement of if % closing statement of for function Structure: function [output1,output2] = practice(input1, input2) output1 = input1 * 5; output2 = input2 * 10; % optional to a function

Simple Graphics Commands: plot(x,y) Plots array y vs. array x plot(x,y, *b ) Plots array y vs. array x with blue * s plot(x,y, *b,x,z, -r ) Plots y vs. x and z vs. x on same graph grid Creates a grid background on graph xlabel( x axis label ) Labels x axis ylabel( y axis label ) Labels y axis title( title name ) Puts title on plot text(gx,gy, string ) Places text string at position (gx,gy) on graph leg( string1, string2,...) Creates leg of multiple plot lines on graph axis ([xmin xmax ymin ymax]) Manually sets axis limits axis auto Automatically sets axis limits (default) axis square Fixes graph in square shape axis equal Fixes graph to equal x and y increments axis normal Resets graph to default mode whitebg( r ) Change graph background color to red hold on Freezes current graph for apping other plots to it. hold off Removes freeze to plot shg Shows graphic window clg Clears graphic window close Closes the graphic window Operators and Other special characters: = Assignment [ ] designates matrix beginning and ( ) parenthesis, brackets subscripts, brackets arguments of functions delimiters for character string, or space separates elements in row of an array ; or return separates rows of an array... continues command on next line % designates following characters as comments : designate a range of values in row * Full matrix multiplication.* Array multiplication Transpose of a matrix

Trigonometric functions: (Trig function use radians, not degrees) cos(x) cosine of x sin(x) sine of x tan(x) tangent of x Common Math Functions: abs(x) absolute value of x exp(x) exponential operation, e x log(x) natural logarithm log10(x) base 10 logarithm rem(x,y) computes remainder of division for x/y - mod(x,y) also works sign(x) generates -1, 0, or 1 based on sign of x sqrt(x) square root of x ceil(x) round x toward +infinity floor(x) round x toward -infinity round(x) round x to nearest integer Utility matrices and matrix fucntions: (m,n are integers, v is vector, A is a matrix) linspace(x,y,m) linearly spaced vector from x to y with m points magic(m) returns an m*m magic square with integers from 1 to m^2 zeros(m,n) returns an m*n matrix of zeros ones(m,n) returns an m*n matrix of ones rand(m,n) returns an m*n matrix of random numbers between 0 and 1 length(v) returns length of vector V size(a) returns size of matrix A as vector [#of row, # of col]

Character String Functions: where S is a string, n is an integer, x is a real number, A is a matrix abs(s) converts characters in string S to their ASCII numeric values char(n) converts ASCII numerical values (integer matix) to character arrays. lower(s) converts any upper case letters in the string S to lower case upper(s) converts lower case letters in the string S to upper case. mat2stsr(a) converts matrix A to a string num2str(x) converts number x to string characters strcmp(s,t) compares two strings S and T, returns 1 if same strncmp(s,t,n) compares first n characters in given strings S and T strcat(s,t) concatenates strings S and T horizontally, ignoring trailing blanks Output to Screen Statements: fprintf( %f \n,m ) fprintf( %i, n) fprintf( %f %f %f \n,x,y,z) fprintf( The string is %s \n,s) prints values of matrix, m with line feed prints values of integer n (%d also works) print out values of x,y, and z on same line print out string S in position %s. Input from Keyboard x=input( enter value for x: ) prompts the user to input a value for variable x S=input( enter a string, s ) prompts the user to input a string to variable S k=menu( title, button1, button2, button3 ) creates menu with 3 button, puts integer in k menu( message, Continue ) creates a button which requires user to click to continue Format field characters for fprint and fscanf commands: Conversion Characters %f fixed point notation %8.4f fixed point with 8 space min, 4 dec. places %-f fixed point notation, left justified %e exponential notional %12.4e exp. notation with 12 space min, 4 dec. places %g either fixed point or exp. notation. %i integer notation %3i integer notation with 3 spaces min %s character string notation Escape Characters \n new line \t horizontal tab \b backspace \r carriage return \f form feed \\ backslash

File Access Commands: fid = fopen( filename.ext, permission ) Opens file with name filename.ext, permissions: fclose(fid) Close file. w write file frewind(fid) Reset file pointer to beginning of file. r read file A=fscanf(fid, %f ) Read floating point numbers from file storing values into column matrix A until all numbers have been read. a app file B=fscanf(fid, %f, 6) Read 6 floating point numbers from file storing values into column matrix B [A,count]=fscanf(fid, %f,[2,inf]) Read floating point numbers from file storing them in 2xN matrix A until all values have been read. fprintf(fid, %f \n, B) Write all values of B matrix into the file, one item per line. fprintf(fid, %f %f %f \n, a,b,c) Write values a, b, and c into the file on same line. ---------------------------------------------------------------------------------------------------------- Example of using fscanf to read in values of x and y from data file: Bob.dat on A drive. fidbob=fopen( A:Bob.dat, r ) A=fscanf(fidBob, %f%f,[2,5]) x = A(1,1:) y = A(2,1:) fclose(fidbob) this gives x = [1 2 3 4 5] and y = [5 10 15 25 20] Bob.dat 1 5 2 10 3 15 4 25 5 20 ----------------------------------------------------------------------------------------------- Example used to read long one dimensional array of unknown size in file: data.txt fida=fopen( data.txt, r ) [A,count]=fscanf(fidA, %f ) fclose(fida) The data gets stored in the column vector A and the variable count is the number of data values read. ---------------------------------------------------------------------------------------------------------------- Example used to write the values of x and y arrays into a file called MyData.txt x = [1 2 3 4 5] y = [ 5 10 35 50 100] fidmy=fopen( MyData.txt, w ) fprintf(fidmy, MyData.txt starts here: ) for i=1:length(x) fprintf(fidmy, %8.4f %for fclose(fidmy) %f \n, x(i), y(i)) MyData.txt starts here: 1.0000 5.0000 2.0000 10.0000 3.0000 35.0000 4.0000 50.0000 5.0000 100.0000 When completed the written file will look like