It would be ridiculous to type in commands at the command line all the time. For example, if you have a series of commands that you want to repeat frequently you would put them in a "script" (i.e., a program) that can be run over and over again.
Let's make a simple script (and your first real program!). In MATLAB, click on the "New Script" button.
Let's make a simple script (and your first real program!). In MATLAB, click on the "New Script" button.
You should see a new window that looks like this:
In the script window, let's type the following commands:
clc
clear all
close all
If you recall, clc clears the Command Window and clear all deletes all the variables that have been created (be careful when you do this!). close all closes all open windows and ports (more on this later, but a good thing to run at the start of a script).
Now, lets add our angle code:
angle = atand(6/4);
hypotenuse = 6/sind(angle);
And one more line:
disp(['The angle is: ' num2str(angle)]);
A note here. There is a very important space between the second ' and num2str. Without it, you will see that num2str is underlined in red. With the space, it is not. That is because without the space MATLAB thinks that this is all one thing.
This last line will write the angle in the Command Window. There is a bit here so let's pull it apart. We know what disp does already - it writes to the command window. Inside the ( ) there are [ ]. disp writes anything between the ( ). However, we want to put some text together, text we write between the two single quotes: 'The angle is: ' and the angle. However, the angle is a numerical variables so we have to turn the "number" into a "string" (or text) - hence num2str(angle). The command takes the number in the variable and converts it into a text string. The [ ] are used to concatenate the two pieces of text - 'The angle is: ' and num2str(angle).
Try the following code in the command window:
disp(['The angle is: ' angle]);
You will note you get the wrong value! That is because MATLAB does not know what to do with angle.
Your script should look like this:
clc
clear all
close all
If you recall, clc clears the Command Window and clear all deletes all the variables that have been created (be careful when you do this!). close all closes all open windows and ports (more on this later, but a good thing to run at the start of a script).
Now, lets add our angle code:
angle = atand(6/4);
hypotenuse = 6/sind(angle);
And one more line:
disp(['The angle is: ' num2str(angle)]);
A note here. There is a very important space between the second ' and num2str. Without it, you will see that num2str is underlined in red. With the space, it is not. That is because without the space MATLAB thinks that this is all one thing.
This last line will write the angle in the Command Window. There is a bit here so let's pull it apart. We know what disp does already - it writes to the command window. Inside the ( ) there are [ ]. disp writes anything between the ( ). However, we want to put some text together, text we write between the two single quotes: 'The angle is: ' and the angle. However, the angle is a numerical variables so we have to turn the "number" into a "string" (or text) - hence num2str(angle). The command takes the number in the variable and converts it into a text string. The [ ] are used to concatenate the two pieces of text - 'The angle is: ' and num2str(angle).
Try the following code in the command window:
disp(['The angle is: ' angle]);
You will note you get the wrong value! That is because MATLAB does not know what to do with angle.
Your script should look like this:
See the green writing? These are comments. You can add your own comments to any program by adding text after a % sign. If you are doing it right, the text will automatically turn to green. Adding comments is a very good idea so others know what your code is doing. Countless times in my lab we have opened up old scripts and have had no idea what they do because there were no comments!
To run your program, click the big run button in the top middle of the command window. You will see a screen that asks you to provide a filename for your program. You can pick any name you want. After you click save, check the command window!
To run your program, click the big run button in the top middle of the command window. You will see a screen that asks you to provide a filename for your program. You can pick any name you want. After you click save, check the command window!
You can run your script any time from the command window my simply typing: my_program
Try this now.
You will note that MATLAB has added a .m to the end of the file name. This is just so the operating system knows the file is associated with MATLAB. A lot of people refer to MATLAB scripts as m-files. Now you know why!
Congratulations, you have written your first script / program / m-file. Note, all of these names are interchangeable (i.e., they mean the same thing).
Onto the next tutorial - 1.5!
Try this now.
You will note that MATLAB has added a .m to the end of the file name. This is just so the operating system knows the file is associated with MATLAB. A lot of people refer to MATLAB scripts as m-files. Now you know why!
Congratulations, you have written your first script / program / m-file. Note, all of these names are interchangeable (i.e., they mean the same thing).
Onto the next tutorial - 1.5!