Foundations of computer programming in MATLAB

The idea of scripts

Scripts are short programs written in high-level programming languages like MATLAB. These are the most common type of MATLAB programs, often written to solve a specific problem. Scripts, like other programs, are written in a text-based file in the syntax of the programming language being used. These are usually saved with a specific file extension, like .c for a C program, .py for a Python program, and .m for a MATLAB program. In the MATLAB integrated development environment (IDE), there is a built-in text editor for .m files—or, as we usually call them—m-files.

One executes a script by handing it to the interpreter. In the MATLAB IDE, we simply click Run in order to execute an m-file. The interpreter evaluates each line of code, successively, until it reaches the end of the file. It adds and manipulates variables in the current Workspace. This is important to note because it will not blow-away old variables saved in the Workspace unless explicitly commanded to do so.

Writing a script

Let’s write a simple script and execute it. Enter the following lines in the editor.

foo = 5;
bar = 7;
baz = foo*bar;
disp(['the product is ',baz])

Save and run it. If prompted, do not add the current directory to the PATH. Instead, change the current directory. What happened?

It didn’t throw an error, but it didn’t do quite what we wanted. We wanted it to print the product, but instead it printed # in its place. This is because we tried to concatenate a string and a double. We must convert baz, first, to a string.

Edit and re-run your script.

Writing the lottery script

So, we need to choose five random people from the class to get a copy of MATLAB for their personal computers. Let’s write a script.

Let’s save an m-file with the following lines at the top:

%%% Script to randomly select five students from the class
clear; close all

Let’s define some data.

%% Data

% Define the number of lucky students
n_lucky = 5;

% Define student data as a cell-array
students = {    ...
  'David L',    ...
  'Claire P',   ...
  'Michael N',  ...
  'Lee S',      ...
  'Ryan H',     ...
  'Scott C',    ...
  'Haitham A',  ...
  'Sirak C',    ...
  'Jamie W',    ...
  'Lance R',    ...
  'Jared R',    ...
  'Mitchell H', ...
  'Andrew Q'    ...
};

% Save the number of students as a variable
n_students = length(students);

% Seed the random number generator ... 
% this allows us to all see the same results (but it's still random)
rng(2349578090) % just keyboard-mashed that integer

Seeding the (pseudo-) random number generator with a positive integer allows us to all synchronize the results.

Now, we make the selection. We use a for loop in order to repeat the same snipet of code n_luck times, selecting a student on each iteration.

%% Selection

% Select by looping through n_lucky times
students_pool = students;
stop_flag = 0;
students_lucky = cell(n_lucky,1);
for i = 1:n_lucky
  % generate a random number between 1 and n_students
  rand_index = ceil(rand(1,1)*length(students_pool)); % index of lucky student
  students_lucky{i} = students_pool{rand_index}; % add lucky student
  disp(['lucky student #',num2str(i),': ',students_lucky{i}])
  % delete the lucky student from the pool
  indices = 1:length(students_pool); % all indices
  indices(rand_index) = []; % delete the lucky index
  students_pool = students_pool(indices); % filtered pool
end

We had to delete the lucky students from the pool because randomness will give us duplicates, otherwise. Here are the lucky winners!

lucky student #1: Jared R
lucky student #2: Jamie W
lucky student #3: Michael N
lucky student #4: Sirak C
lucky student #5: Ryan H

The m-file can be found here