- Engineering & Innovation Home
- IDEA
- Capstone
- Data Science
- Innovation and Entrepreneurship
- Coding and Development Tools
- Design
- SETS Support

- MATLAB: a practical introduction to programming and problem solving by Attaway, StormyCall Number: eBook - ScienceDirectPublication Date: 2019

Type MATLAB in the search box for complete list.

- MATLAB for beginnersLearning MATLAB with Steven Moser

1h 13m - MATLAB - IntermediateMATLAB 2018 Essential Training

with Curt Fyre

3h 15m

**Good for:**

- Data analysis
- Simple plotting
- Parallel processing/bigger data

**Why Matlab:**

- Excellent documentation
- Extensible toolboxes offer wide capabilities
- Used in academia for scientific study/analysis

**Recommended setups:**

- Access to Matlab Virtual Suite through UIUC Citrix Workspace client
- Run locally if you pay ~$100 for a student license
- Use Octave (open source) if you don’t need special libraries or custom functionality

You can install Octave on MacOS, Linux or Windows. It can be run as a GUI (looks kind of similar to Matlab) or directly from the command line.

**Best resource to learn quickly:**

Matlab On-Ramp (create free account with illinois.edu email)

- Matlab’s official on-ramp to working with Matlab. Broken up by task/topic, these videos really help get started fast. Mathworks/Matlab are known for having very good, useful documentation, making it easier to start building new projects with advanced functions/objectives.
- Average video length: ~10 min
- Total duration: 2h15m

In Octave, additional functionality is achieved through the download and installation of Packages. Then, at each time you start Octave, you need to load the package much like a `library`

in R or Python.

In [ ]:

```
# To install a package for Octave, download the package image to your computer.
# Then run:
pkg install pkgname-1.0.0.tar.gz;
# after the package installs, you use it as:
pkg load pkgname;
```

How to use functions from other files

In [49]:

```
# to access a function from another script located in the same folder,
# simply treat the name of the script like a function.
# Note: Only the first (outermost) function will be called from another script
# multiple functions can be defined in the other script, but they can't be accessed directly
# from another script. Look at the other_script.m file to understand this.
# You'll also get a warning the first time if the top function doesn't match the filename,
# but it will still run the first function no matter what it's called.
# In this example, other_script.m has two functions:
# other_function(a,b) and helper(n).
# other_function(a,b) calls function helper(n) to print out whatever 'a' was,
# then returns a + (b/2).
# This is what happens when we call other_script like a function:
res = other_script(2,4)
# Note you can call it again too.
res2 = other_script(1,8)
```

you entered: 2 res = 4 you entered: 1 res2 = 5

How to import data into Matlab. Very useful for previewing and processing all kinds of information.

In [33]:

```
# For well organized, purely numerical data (NO TEXT), you have two choices.
# For csv files containing numbers only, you can use csvread()
#csvdata = csvread('Path/to/file.csv', delimiter, rows, columns)
# If you have saved a variable/matrix to a .mat file, you can read it into Matlab with the load() function:
#matlab_data = load('path/to/mat/file.mat');
# if you are okay reading in without interpretation of data headers, just call import data on the file.
data2 = importdata('data/ExampleDataClean2.csv');
data2(1:10)
# to read into a table/matrix with column labels, use
#data = readtable('data/ExampleDataClean2.csv')
```

```
# For Octave, reading in the importdata() with headers allows Octave to determine numerical data from text.
# for this reason, it dumps the data in a struct which you can unpack as follows:
# if you have dirty data (not all numeric, unequal rows/columns), read in your data with importdata()
# the second parameter is the delimeter (CSV uses commas, TSV uses \t [tab], and still others use semicolons)
data = importdata('data/ExampleDataClean2.csv', ',', 1);
typeinfo(data)
all_fields = fieldnames(data)
data.textdata(1:10)
data.data(1:10)
```

ans = scalar struct all_fields = { [1,1] = data [2,1] = textdata } ans = { [1,1] = Timestamp,Val1,Val2,Val3 [2,1] = 15:50:40.94 [3,1] = 7741335 pig attached [4,1] = 15:50:41.08 [5,1] = 7741335 pig attached [6,1] = 15:50:41.23 [7,1] = 7741335 pig attached [8,1] = 15:50:41.38 [9,1] = 7741335 pig attached [10,1] = 15:50:41.51 } ans = Columns 1 through 8: -0.65625 -0.56250 -1.26562 -0.65625 1.06250 -1.29688 -0.35938 0.45312 Columns 9 and 10: 0.26562 -0.25000

How to read in data from an excel data file

In [44]:

```
# In Octave, the Matlab 'table' datatype is not implemented.
# This means mixed data (ie, text and numbers) takes a bit more effort to work with.
# An example is shown below that highlights how you
pkg load io
pkg load dataframe
# in Matlab, you should use readtable() or readcell(). Although xlsread will still work as of 2020.
A = xlsread('data/ExampleDataClean.xlsx');
typeinfo(A)
B = datestr(A(:,1), f=2);
C = [num2cell(B, 2), num2cell(A(:,2:end))];
D = dataframe(C);
A(1:5,:) # data read in. If it can be converted to number it is. If only text, it shows up as NaN.
B(1:5,:) # first column converted to date strings.
C(1:5,:) # to blend text and numerical data, use a cell array. each cell can hold different types of data
D(1:5,:) # there's also a dataframe in Octave, which is the closest thing currently to a Matlab table.
```

ans = matrix ans = Columns 1 through 5: 43957.000000 NaN 0.087072 0.466163 0.256585 43958.000000 NaN 0.374148 0.596247 0.460567 43960.000000 NaN 0.950790 0.786810 0.375649 43962.000000 NaN 0.956982 0.137989 0.949380 43964.000000 NaN 0.275037 0.759614 0.623671 Columns 6 through 10: 0.850972 0.024691 0.170840 0.306948 0.224802 0.455865 0.473421 0.037419 0.164160 0.317844 0.651548 0.224263 0.364410 0.277153 0.687860 0.251682 0.422171 0.782476 0.938242 0.237547 0.096792 0.265659 0.174680 0.850576 0.956483 Columns 11 and 12: 0.227992 0.423540 0.357118 0.233253 0.586596 0.062513 0.456039 0.619374 0.336973 0.135804 ans = 05/07/20 05/08/20 05/10/20 05/12/20 05/14/20 ans = { [1,1] = 05/07/20 [2,1] = 05/08/20 [3,1] = 05/10/20 [4,1] = 05/12/20 [5,1] = 05/14/20 [1,2] = NaN [2,2] = NaN [3,2] = NaN [4,2] = NaN [5,2] = NaN [1,3] = 0.087072 [2,3] = 0.37415 [3,3] = 0.95079 [4,3] = 0.95698 [5,3] = 0.27504 [1,4] = 0.46616 [2,4] = 0.59625 [3,4] = 0.78681 [4,4] = 0.13799 [5,4] = 0.75961 [1,5] = 0.25658 [2,5] = 0.46057 [3,5] = 0.37565 [4,5] = 0.94938 [5,5] = 0.62367 [1,6] = 0.85097 [2,6] = 0.45587 [3,6] = 0.65155 [4,6] = 0.25168 [5,6] = 0.096792 [1,7] = 0.024691 [2,7] = 0.47342 [3,7] = 0.22426 [4,7] = 0.42217 [5,7] = 0.26566 [1,8] = 0.17084 [2,8] = 0.037419 [3,8] = 0.36441 [4,8] = 0.78248 [5,8] = 0.17468 [1,9] = 0.30695 [2,9] = 0.16416 [3,9] = 0.27715 [4,9] = 0.93824 [5,9] = 0.85058 [1,10] = 0.22480 [2,10] = 0.31784 [3,10] = 0.68786 [4,10] = 0.23755 [5,10] = 0.95648 [1,11] = 0.22799 [2,11] = 0.35712 [3,11] = 0.58660 [4,11] = 0.45604 [5,11] = 0.33697 [1,12] = 0.42354 [2,12] = 0.23325 [3,12] = 0.062513 [4,12] = 0.61937 [5,12] = 0.13580 } ans = dataframe with 5 rows and 12 columns _1 C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 Nr char double double double double double double double double double double double 1 05/07/20 NaN 0.087072 0.46616 0.25658 0.850972 0.024691 0.170840 0.30695 0.22480 0.22799 0.423540 2 05/08/20 NaN 0.374148 0.59625 0.46057 0.455865 0.473421 0.037419 0.16416 0.31784 0.35712 0.233253 3 05/10/20 NaN 0.950790 0.78681 0.37565 0.651548 0.224263 0.364410 0.27715 0.68786 0.58660 0.062513 4 05/12/20 NaN 0.956982 0.13799 0.94938 0.251682 0.422171 0.782476 0.93824 0.23755 0.45604 0.619374 5 05/14/20 NaN 0.275037 0.75961 0.62367 0.096792 0.265659 0.174680 0.85058 0.95648 0.33697 0.135804

How to generate a basic data plot

In [63]:

```
# To plot data, use the plot() function.
# we'll demonstrate by plotting from a csv file.
pkg load io
A = xlsread('data/ExampleDataClean.xlsx');
# plot the third and fourth columns as time series
# first, generate the 'x' values. Our data are columns,
# so we need to match the dimensions, hence the transpose
x = transpose(linspace(1, 60, size(A, 1)));
# Now we plot the third and fourth columns.
# The third column will be a blue line, and the second red stars.
plot(x, A(:,3), 'b-', x, A(:,4), 'r*')
```

A fully functional code snippet that may be useful.

In [ ]:

```
# here is a function for reading in WFDB format files from physionet (raw EMG data, for example)
function [times, data] = readWFDB(filename)
fid = fopen(filename);
% get HEA info like offset, format, frequency, gain
offset = 20;
gain = 500;
sample_frequency = 23437.5;
precision = "int16";
format = 'l'; % little endian
data = fread(fid, Inf, precision, 0, format);
data = data([offset:end],:);
data(:,1) = data(:,1) ./ gain;
len = size(data,1);
times = linspace(0, len -1, len)';
times = times./sample_frequency;
fclose(fid);
end
```

- Last Updated: Mar 2, 2023 10:14 AM
- URL: https://guides.library.illinois.edu/enginnovation
- Print Page