logging in or signing up L6 10 30 2003 miloung Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINTLite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 389 Category: Entertainment License: All Rights Reserved Like it (0) Dislike it (0) Added: November 05, 2007 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Introduction to MATLAB: Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/29/2003Overview for 10/30/2003: Overview for 10/30/2003 Brief review of topics covered in last session (10/29/2003) MATLAB Functions Looping! Optimization Review of topics covered thus farReview for 10/29/2003: Review for 10/29/2003 File Output MATLAB Scripts and FunctionsHigh Level File Output: High Level File Output Some of the input commands have corresponding high-level output commands csvwrite dlmwritecsvwrite: csvwrite Write a matrix to a comma-seperated value file Syntax: csvwrite('filename',M) csvwrite('filename',M,row,col) Ex. csvwrite(‘blah.csv’,a); dlmwrite: dlmwrite Writes a matrix to a delimited file (using the delimiter you specify) Syntax: dlmwrite(filename,M,delimiter) dlmwrite(filename,M,delimiter,row,col) Ex. dlmwrite(‘blah.txt’,a,’:’);Low-Level file I/O: Low-Level file I/O fopen fclose fprintf fgetl / fgetsfopen: fopen Opens a file and returns the handle to the file object File_ID = fopen(‘blah.txt’) Capturing the file handle is necessary to write or read to/from the filefclose: fclose Closes a file associated with a specific file identification handle Ex. fclose(File_ID); Ex. fclose(‘all’);fprintf: fprintf Multi-use: can output to a file or a screen Ex. fprintf(fid,'%6.2f %12.8f\n',y); %6.2f means a floating point with 6 leading decimals and 2 trailing Specifying 1 instead of fid will output to the screenfgetl / fgets: fgetl / fgets Get line and get string, respectively. Fgetl will get you a line without the newline character at the end, while fgets will preserve the newline character (\n). Syntax: Line = fgetl(File_ID); Line = fgets(File_ID);Programming in MATLAB: Programming in MATLAB Two types of files: Scripts FunctionsMATLAB Scripts: MATLAB Scripts Scripts are MATLAB commands stored in text files. When you type the name of the script file at the MATLAB prompt the commands in the script file are executed as if you had typed them in from the keyboard. Scripts end with the extension .m Referred to as M-FilesMATLAB Functions: MATLAB Functions Functions are similar to scripts Functions may take arguments Functions may return one or more valuesMATLAB Functions, con’t: 2: MATLAB Functions, con’t: 2 function [output] = function_name(input_arguments) The above is a function header and should be the first non-comment line in the function file Comments may be placed above the function headerMATLAB Functions, con’t: 3: MATLAB Functions, con’t: 3 Example function function [output] = square(input) output = input*input; Body of functions can contain code just like scripts couldLooping!: Looping! Scripts and functions also allow the ability to loop using conventional for and while loops. Note that the interpreter also lets you do it, it is simply less easy to graspFor Loops: For Loops Common to other programming languages for variable = expression statement ... statement end For Loops, con’t: 2: For Loops, con’t: 2 Example: (taken from MATLAB help) a = zeros(k,k) % Preallocate matrix for m = 1:k for n = 1:k a(m,n) = 1/(m+n -1); end end For Loops, con’t: 3: For Loops, con’t: 3 The looping variable is defined in much the same way that we defined arrays/vectors. Ex. m = 1:k Or m = 1:10For Loops, con’t: 4: For Loops, con’t: 4 Loops are shown to end by the keyword “end” Curly braces are not present to subdivide packets of code Make use of adequate white-space and tabbing to improve code readabilityWhile Loops: While Loops Similar to while loops in other languages while expression statement … end While Loops, con’t: 2: While Loops, con’t: 2 Ex. (taken from help while) while (1+eps) > 1 eps = eps/2; end While Loops, con’t: 3: While Loops, con’t: 3 Same notes apply to while loops. Code is separated by the keyword “end”Looping conclusion: Looping conclusion Some other aspects of looping exist Use help while and help for to see themMATLAB Code Optimization: MATLAB Code Optimization Two ways to optimize MATLAB code Vectorize code Preallocate matrices Information contained here: http://www.mathworks.com/access/helpdesk_r12p1/help/techdoc/matlab_prog/ch10_p47.shtmlReview: Review Week 1: MATLAB GUI Simple Commands Declaring Variables Simple functions Week 2: Plotting File Input File Output MATLAB GUI: MATLAB GUI Launch Pad / Toolbox Workspace Current Directory Command History Command WindowSimple Commands: Simple Commands who whos save clear loadDeclaring a variable in MATLAB: Declaring a variable in MATLAB Not necessary to specify a type. (Such as int or float) Several kinds of variables: Vector Matrix Structure Cell arrayDeclaring a variable, con’t: 2: Declaring a variable, con’t: 2 For an integer or floating point number: simply set a variable name equal to some character Ex. A = 5; Or A = 5Sample MATLAB functions: Sample MATLAB functions Min Max Median Mean Sum DiffPlotting: Plotting Several types of plots available Plot Polar Bar HistPlot() con’t: 3 – Color options: Plot() con’t: 3 – Color options Color options: Yellow - ‘y’ Magenta - ‘m’ Cyan - ‘c’ Red - ‘r’ Green - ‘g’ Blue - ‘b’ White - ‘w’ Black - ‘k’ Example: plot(temp, ‘y’);Plot() con’t: 4 – Line options: Plot() con’t: 4 – Line options Line styles: - solid line (default) -- dashed line : dotted line -. dash-dot lineHigh-Level File I/O: High-Level File I/O I/O = input/output 3 important commands for input csvread dlmread textreadLook Ahead: Look Ahead Next week: Review of any topics requested Profiling your code for speed Each day will have a separate part of the projectEnd: End Happy Halloween! You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
L6 10 30 2003 miloung Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINTLite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 389 Category: Entertainment License: All Rights Reserved Like it (0) Dislike it (0) Added: November 05, 2007 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Introduction to MATLAB: Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/29/2003Overview for 10/30/2003: Overview for 10/30/2003 Brief review of topics covered in last session (10/29/2003) MATLAB Functions Looping! Optimization Review of topics covered thus farReview for 10/29/2003: Review for 10/29/2003 File Output MATLAB Scripts and FunctionsHigh Level File Output: High Level File Output Some of the input commands have corresponding high-level output commands csvwrite dlmwritecsvwrite: csvwrite Write a matrix to a comma-seperated value file Syntax: csvwrite('filename',M) csvwrite('filename',M,row,col) Ex. csvwrite(‘blah.csv’,a); dlmwrite: dlmwrite Writes a matrix to a delimited file (using the delimiter you specify) Syntax: dlmwrite(filename,M,delimiter) dlmwrite(filename,M,delimiter,row,col) Ex. dlmwrite(‘blah.txt’,a,’:’);Low-Level file I/O: Low-Level file I/O fopen fclose fprintf fgetl / fgetsfopen: fopen Opens a file and returns the handle to the file object File_ID = fopen(‘blah.txt’) Capturing the file handle is necessary to write or read to/from the filefclose: fclose Closes a file associated with a specific file identification handle Ex. fclose(File_ID); Ex. fclose(‘all’);fprintf: fprintf Multi-use: can output to a file or a screen Ex. fprintf(fid,'%6.2f %12.8f\n',y); %6.2f means a floating point with 6 leading decimals and 2 trailing Specifying 1 instead of fid will output to the screenfgetl / fgets: fgetl / fgets Get line and get string, respectively. Fgetl will get you a line without the newline character at the end, while fgets will preserve the newline character (\n). Syntax: Line = fgetl(File_ID); Line = fgets(File_ID);Programming in MATLAB: Programming in MATLAB Two types of files: Scripts FunctionsMATLAB Scripts: MATLAB Scripts Scripts are MATLAB commands stored in text files. When you type the name of the script file at the MATLAB prompt the commands in the script file are executed as if you had typed them in from the keyboard. Scripts end with the extension .m Referred to as M-FilesMATLAB Functions: MATLAB Functions Functions are similar to scripts Functions may take arguments Functions may return one or more valuesMATLAB Functions, con’t: 2: MATLAB Functions, con’t: 2 function [output] = function_name(input_arguments) The above is a function header and should be the first non-comment line in the function file Comments may be placed above the function headerMATLAB Functions, con’t: 3: MATLAB Functions, con’t: 3 Example function function [output] = square(input) output = input*input; Body of functions can contain code just like scripts couldLooping!: Looping! Scripts and functions also allow the ability to loop using conventional for and while loops. Note that the interpreter also lets you do it, it is simply less easy to graspFor Loops: For Loops Common to other programming languages for variable = expression statement ... statement end For Loops, con’t: 2: For Loops, con’t: 2 Example: (taken from MATLAB help) a = zeros(k,k) % Preallocate matrix for m = 1:k for n = 1:k a(m,n) = 1/(m+n -1); end end For Loops, con’t: 3: For Loops, con’t: 3 The looping variable is defined in much the same way that we defined arrays/vectors. Ex. m = 1:k Or m = 1:10For Loops, con’t: 4: For Loops, con’t: 4 Loops are shown to end by the keyword “end” Curly braces are not present to subdivide packets of code Make use of adequate white-space and tabbing to improve code readabilityWhile Loops: While Loops Similar to while loops in other languages while expression statement … end While Loops, con’t: 2: While Loops, con’t: 2 Ex. (taken from help while) while (1+eps) > 1 eps = eps/2; end While Loops, con’t: 3: While Loops, con’t: 3 Same notes apply to while loops. Code is separated by the keyword “end”Looping conclusion: Looping conclusion Some other aspects of looping exist Use help while and help for to see themMATLAB Code Optimization: MATLAB Code Optimization Two ways to optimize MATLAB code Vectorize code Preallocate matrices Information contained here: http://www.mathworks.com/access/helpdesk_r12p1/help/techdoc/matlab_prog/ch10_p47.shtmlReview: Review Week 1: MATLAB GUI Simple Commands Declaring Variables Simple functions Week 2: Plotting File Input File Output MATLAB GUI: MATLAB GUI Launch Pad / Toolbox Workspace Current Directory Command History Command WindowSimple Commands: Simple Commands who whos save clear loadDeclaring a variable in MATLAB: Declaring a variable in MATLAB Not necessary to specify a type. (Such as int or float) Several kinds of variables: Vector Matrix Structure Cell arrayDeclaring a variable, con’t: 2: Declaring a variable, con’t: 2 For an integer or floating point number: simply set a variable name equal to some character Ex. A = 5; Or A = 5Sample MATLAB functions: Sample MATLAB functions Min Max Median Mean Sum DiffPlotting: Plotting Several types of plots available Plot Polar Bar HistPlot() con’t: 3 – Color options: Plot() con’t: 3 – Color options Color options: Yellow - ‘y’ Magenta - ‘m’ Cyan - ‘c’ Red - ‘r’ Green - ‘g’ Blue - ‘b’ White - ‘w’ Black - ‘k’ Example: plot(temp, ‘y’);Plot() con’t: 4 – Line options: Plot() con’t: 4 – Line options Line styles: - solid line (default) -- dashed line : dotted line -. dash-dot lineHigh-Level File I/O: High-Level File I/O I/O = input/output 3 important commands for input csvread dlmread textreadLook Ahead: Look Ahead Next week: Review of any topics requested Profiling your code for speed Each day will have a separate part of the projectEnd: End Happy Halloween!