1 / 10

Introduction to MATLAB Scripts: Efficient Coding and Running Programs

Learn how to create, edit, save, and run MATLAB scripts to streamline your coding process. Understand script naming conventions, handling errors, and clearing the command window effectively.

jimmyt
Download Presentation

Introduction to MATLAB Scripts: Efficient Coding and Running Programs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scripts

  2. Scripts (m-file) Typing everything in the command window is inefficient Put all the commands/calculations we want to run in a script or ‘m-file’ instead. Allows us to: • Edit the commands • Save the commands • Run them all at once • Print out the list of commands on paper Script = ‘m-file’ = PROGRAM!

  3. Opening a New Script (m-file)

  4. Scripting Now we can type the lines of code: Notice that nothing happens yet – you’re just typing away

  5. Running Scripts Click ‘Run’ once you have all your commands entered • Command saves AND THEN runs

  6. Saving Scripts When you click ‘run’, MATLAB prompts you to name and save your m-file in your working directory: Good Practice: Pick a ‘good’ filename. Don’t pick untitled7.m

  7. File Naming Practices MATLAB names must start with a letter and contain only letters, numbers or underscores

  8. Running Scripts After you name the file, MATLAB tries to the run the script If all correct: • File name and all of the “ans = “ stuff prints in the command window • Text will appear in the Day1_Ex1.txt file (That’s where we told MATLAB to put the text)

  9. Presence of Errors If mistakes are present • MATLAB ‘dings’ at you • Red words appear in the command window • READ THE WORDS.Tells you what line number has the mistake • Fix mistakes the script • Click run again (changes are saved before script is executed) • Rinse and repeat until all mistakes are repeated • Can be helpful to clear the command window to see the new mistakes clearly

  10. Clearing the Command Window If the command window gets cluttered • Right-click in the command window and choose “clear command window” OR • Type ‘clc’ in command window OR • Insert ‘clc’ as the first line in your script • This will clear the window with every run

More Related