KRIGOLSON TEACHING
  • NEUROSCIENCE
    • NEUROSCIENCE 100 >
      • NEURO 100 INTRODUCTION
      • NEURO 101 ADVANCED
      • NEURO 102 AGING
      • NEURO 103 MEMORY
      • NEURO 104 DECISION MAKING
      • NEURO 105 LEARNING
      • Research Statistics
    • NRSC 500B / MEDS 470
  • Kinesiology
    • EPHE 245
    • EPHE 357
  • STATISTICS
    • BIOMEDICAL STATISTICS
    • MULTIVARIATE STATISTICS >
      • MULTIPLE REGRESSION
    • RESOURCES
    • R TIPS
  • MATLAB
    • THE BASICS >
      • Hello World
      • BASIC MATHEMATICS
      • VARIABLES
      • Matrices
      • Writing Scripts
      • PATHS AND DIRECTORIES
      • USER INPUT
      • FOR LOOPS
      • WHILE LOOPS
      • IF STATEMENTS
      • RANDOM NUMBERS
    • STATISTICS >
      • LOADING DATA
      • DESCRIPTIVE STATISTICS
      • MAKING FUNCTIONS
      • BAR GRAPHS
      • LINE GRAPHS
      • TTESTS
    • EXPERIMENTS: THE BASICS >
      • DRAWING A CIRCLE
      • DRAWING MULTIPLE OBJECTS
      • DRAWING TEXT
      • DRAWING AN IMAGE
      • PLAYING A TONE
      • KEYBOARD INPUT
      • BUILDING A TRIAL
      • BUILDING TRIALS
      • NESTED LOOPS
      • RIGHT OR WRONG
      • SAVING DATA
    • EXPERIMENTS: ADVANCED >
      • STROOP
      • N BACK
      • Oddball
      • Animation
      • VIDEO
    • EEG and ERP Analysis >
      • ERP Analysis
      • FFT Analysis
      • Wavelet Analysis
  • Directed Studies
    • Advanced Topics in Motor Control A
    • Advanced Topics in Motor Control B
    • An Introduction to EEG
    • Advanced EEG and ERP Methods
    • Neural Correlates of Human Reward Processing
    • Independent Research Project
  • RESOURCES
    • EXCEL
    • HOW TO READ A RESEARCH PAPER
    • HOW TO WRITE A RESEARCH PAPER
  • Workshops
    • Iowa State EEG Workshop 2018
  • Python
    • The Basics >
      • Setting Up Python
      • Hello, world!
      • Basic Math & Using Import
      • Variables
      • Matrices
      • Scripts
      • User Input
      • For Loops

1.4 Writing Scripts

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.

Picture
You should see a new window that looks like this:
Picture
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:
Picture
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!
Picture
Picture
Picture
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!
  • NEUROSCIENCE
    • NEUROSCIENCE 100 >
      • NEURO 100 INTRODUCTION
      • NEURO 101 ADVANCED
      • NEURO 102 AGING
      • NEURO 103 MEMORY
      • NEURO 104 DECISION MAKING
      • NEURO 105 LEARNING
      • Research Statistics
    • NRSC 500B / MEDS 470
  • Kinesiology
    • EPHE 245
    • EPHE 357
  • STATISTICS
    • BIOMEDICAL STATISTICS
    • MULTIVARIATE STATISTICS >
      • MULTIPLE REGRESSION
    • RESOURCES
    • R TIPS
  • MATLAB
    • THE BASICS >
      • Hello World
      • BASIC MATHEMATICS
      • VARIABLES
      • Matrices
      • Writing Scripts
      • PATHS AND DIRECTORIES
      • USER INPUT
      • FOR LOOPS
      • WHILE LOOPS
      • IF STATEMENTS
      • RANDOM NUMBERS
    • STATISTICS >
      • LOADING DATA
      • DESCRIPTIVE STATISTICS
      • MAKING FUNCTIONS
      • BAR GRAPHS
      • LINE GRAPHS
      • TTESTS
    • EXPERIMENTS: THE BASICS >
      • DRAWING A CIRCLE
      • DRAWING MULTIPLE OBJECTS
      • DRAWING TEXT
      • DRAWING AN IMAGE
      • PLAYING A TONE
      • KEYBOARD INPUT
      • BUILDING A TRIAL
      • BUILDING TRIALS
      • NESTED LOOPS
      • RIGHT OR WRONG
      • SAVING DATA
    • EXPERIMENTS: ADVANCED >
      • STROOP
      • N BACK
      • Oddball
      • Animation
      • VIDEO
    • EEG and ERP Analysis >
      • ERP Analysis
      • FFT Analysis
      • Wavelet Analysis
  • Directed Studies
    • Advanced Topics in Motor Control A
    • Advanced Topics in Motor Control B
    • An Introduction to EEG
    • Advanced EEG and ERP Methods
    • Neural Correlates of Human Reward Processing
    • Independent Research Project
  • RESOURCES
    • EXCEL
    • HOW TO READ A RESEARCH PAPER
    • HOW TO WRITE A RESEARCH PAPER
  • Workshops
    • Iowa State EEG Workshop 2018
  • Python
    • The Basics >
      • Setting Up Python
      • Hello, world!
      • Basic Math & Using Import
      • Variables
      • Matrices
      • Scripts
      • User Input
      • For Loops