Introduction to MATLAB Scripts: Efficient Coding and Running Programs
100 likes | 123 Vues
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.
Introduction to MATLAB Scripts: Efficient Coding and Running Programs
E N D
Presentation Transcript
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!
Scripting Now we can type the lines of code: Notice that nothing happens yet – you’re just typing away
Running Scripts Click ‘Run’ once you have all your commands entered • Command saves AND THEN runs
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
File Naming Practices MATLAB names must start with a letter and contain only letters, numbers or underscores
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)
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
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