Introduction
The purpose of this lab is to familiarize students with the basics of Matlab by reading through its
documentation and to implement a Matlab program that will take an input bit string of length n > 0,
which is assumed to be encoded using n-bit two's complement, and decode it to a base-10 integer. We
will use Matlab R2021a for this lab assignment.
Part 1
Before we jump into any programming in Matlab, you should first understand some Matlab code that you
may need for this assignment. For each of the Matlab functions or programming constructs below, you
should read over what they do in Matlab's documentation and practice them in a shell. Take the
necessary time to understand the syntax, what each thing does below, and how to use them.
1. variables, assignment operator (=), and arithmetic operators
2. not ending a Matlab statement with a semicolon to show output
3. ending a Matlab statement with a semicolon to suppress output
4. if, elseif, else
5. relational operators (==, >=, >, <=, <, ~=)
important note∶ the == operator is different than the = operato
6. working with strings (as a char a
ay in Matlab)∶
s = '011001'
n = strlength(s)
s(n), s(n-1), …, s(2), s(1)
s(n) == '1', s(n-1) == '1', …, s(2) == '1' , s(1) == '1'
7. while
8. fo
9. fprintf
Part 2
After you are familiar with some Matlab programming from the previous part, you should create a folde
(perhaps on your Desktop) called download the file decode.m from eLC, and place that file in
your folder. You may edit .m files in Matlab or use a simple text editor. Open the .m file and
ead through the comments and implement the function(s) in that file.
The .m file contains the co
ect syntax
for declaring functions. For this lab assignment, do NOT use any built-in functions in Matlab that
automatically convert a bit string (or any binary representation) to an integer (using such a function will
esult in a zero on this assignment). Also, do NOT use the flip function in Matlab.
In computer science, it is common to run programs using command line arguments. We will do this fo
this lab's program. To run the program using the command line, you should open up a command prompt
(Windows) or terminal (Mac) and cd into your (ask your TA if you need help with this).
We will run the program for this assignment with one command line argument as shown in the examples.
After you finish co
ectly implementing the .m file, your program's input and output must look like the
following examples (this is a requirement). Each example is a separate run of a co
ectly working
program. Your program must work for any valid command line argument, which you may assume to be a
it string of length n > 0.
Examples
matlab -batch "decode 0";
Assume 0 represents an integer encoded with 1-bit two's complement
0 decoded as a base-10 integer is 0
matlab -batch "decode 1";
Assume 1 represents an integer encoded with 1-bit two's complement
1 decoded as a base-10 integer is -1
matlab -batch "decode 00";
Assume 00 represents an integer encoded with 2-bit two's complement
00 decoded as a base-10 integer is 0
matlab -batch "decode 01";
Assume 01 represents an integer encoded with 2-bit two's complement
01 decoded as a base-10 integer is 1
matlab -batch "decode 10";
Assume 10 represents an integer encoded with 2-bit two's complement
10 decoded as a base-10 integer is -2
matlab -batch "decode 11";
Assume 11 represents an integer encoded with 2-bit two's complement
11 decoded as a base-10 integer is -1
matlab -batch "decode XXXXXXXXXX";
Assume XXXXXXXXXX represents an integer encoded with 8-bit two's complement
XXXXXXXXXX decoded as a base-10 integer is 89
matlab -batch "decode XXXXXXXXXX";
Assume XXXXXXXXXX represents an integer encoded with 8-bit two's complement
XXXXXXXXXX decoded as a base-10 integer is -39
matlab -batch "decode XXXXXXXXXX";
Assume XXXXXXXXXX represents an integer encoded with 13-bit two's complement
XXXXXXXXXX decoded as a base-10 integer is 2551
matlab -batch "decode XXXXXXXXXX";
Assume XXXXXXXXXX represents an integer encoded with 13-bit two's complement
XXXXXXXXXX decoded as a base-10 integer is -1929
Submission
Before submitting a program to us for grading, you must test that your program works co
ectly with the
examples provided and other examples you run and work out by hand (not provided in this document).
Once you co
ectly implement an algorithm, it should work for valid inputs.