Introduction to Matlab
Contents
- See what is in the workspace.
- Delete the second element.
- Display the vector x.
- Find the elements that are negative.
- Print the vector ind to the command window.
- Create a cell array, where one cell contains
- Let’s check the size of the cell array
- Create a structure called employee with three fields.
- Create a table using all four variables.
- See what is in the workspace.
- Extract the employee's area of interest.
- Display the contents in the window.
- Display Wendy's score.
- Get MoonJung's area.
- Get a partial table by extracting the first
- Get the JanTemp variable.
- See if it is equal to the JanTemp variable.
- We get an answer of 1, indicating they are the same.
- First load the data back into the workspace.
- Now, we use the semicolon to stack
- Now look at the workspace to see what is there now.
- Check on the size of irisAll.
- Load the data if not in the workspace.
- Use commas to concatenate as a row.
- Check the workspace.
- Verify the size of UStemps:
- Save the data for the UStemps to a .mat file.
- That used the command syntax to call a function.
- See what is in our directory now.
First see what is in the workspace. This command lists all variables in the workspace.
who
The workspace is empty and nothing is returned. Now load the iris.mat file.
load iris
What is in the workspace now?
who
Your variables are: setosa versicolor virginica
Now save the variables in another file. We will save just the setosa variable object. Use save filename varname.
save setosa setosa
See what files are in the current directory.
dir
. Chapter4.m Chapter8.m .. Chapter5.m Introduction.m Chapter2.m Chapter6.m html Chapter3.m Chapter7.m setosa.mat
%Remove objects from workspace to clean it up.
clear
%Load the earth data. % load DensityEarth.txt -ascii
See what is in the workspace.
who
Your variables are: DensityEarth
Create a vector x.
x = [2, 4, 6];
Delete the second element.
x(2) = [];
Display the vector x.
disp(x)
2 6
Find the elements that are negative.
ind = find(x < 0);
Print the vector ind to the command window.
ind
ind = Empty matrix: 1-by-0
Create a cell array, where one cell contains
numbers and another cell element is a string.
cell_arry2 = {[1,2], 'This is a string'};
Let’s check the size of the cell array
size(cell_arry2)
ans = 1 2
Create a structure called employee with three fields.
employee = struct(... 'name',{{'Wendy','MoonJung'}},... 'area',{{'Visualization','Inference'}},... 'deg',{{'PhD','PhD'}},... 'score',[90 100]) all_names = employee.name
employee = name: {'Wendy' 'MoonJung'} area: {'Visualization' 'Inference'} deg: {'PhD' 'PhD'} score: [90 100] all_names = 'Wendy' 'MoonJung'
load UStemps
Create a table using all four variables.
UTs_tab = table(City,JanTemp,Lat,Long)
UTs_tab = City JanTemp Lat Long ____________________ _______ ____ _____ 'Mobile, AL' 44 31.2 88.5 'Montgomery, AL' 38 32.9 86.8 'Phoenix, AZ' 35 33.6 112.5 'Little Rock, AR' 31 35.4 92.8 'Los Angeles, CA' 47 34.3 118.7 'San Francisco, CA' 42 38.4 123 'Denver, CO' 15 40.7 105.3 'New Haven, CT' 22 41.7 73.4 'Wilmington, DE' 26 40.5 76.3 'Washington, DC' 30 39.7 77.5 'Jacksonville, FL' 45 31 82.3 'Key West, FL' 65 25 82 'Miami, FL' 58 26.3 80.7 'Atlanta, GA' 37 33.9 85 'Boise, ID' 22 43.7 117.1 'Chicago, IL' 19 42.3 88 'Indianapolis, IN' 21 39.8 86.9 'Des Moines, IA' 11 41.8 93.6 'Wichita, KS' 22 38.1 97.6 'Louisville, KY' 27 39 86.5 'New Orleans, LA' 45 30.8 90.2 'Portland, ME' 12 44.2 70.5 'Baltimore, MD' 25 39.7 77.3 'Boston, MA' 23 42.7 71.4 'Detroit, MI' 21 43.1 83.9 'Minneapolis, MN' 2 45.9 93.9 'St. Louis, MO' 24 39.3 90.5 'Helena, MT' 8 47.1 112.4 'Omaha, NE' 13 41.9 96.1 'Concord, NH' 11 43.5 71.9 'Atlantic City, NJ' 27 39.8 75.3 'Albuquerque, NM' 24 35.1 106.7 'Albany, NY' 14 42.6 73.7 'New York, NY' 27 40.8 74.6 'Charlotte, NC' 34 35.9 81.5 'Raleigh, NC' 31 36.4 78.9 'Bismarck, ND' 0 47.1 101 'Cincinnati, OH' 26 39.2 85 'Cleveland, OH' 21 42.3 82.5 'Oklahoma City, OK' 28 35.9 97.5 'Portland, OR' 33 45.6 123.2 'Harrisburg, PA' 24 40.9 77.8 'Philadelphia, PA' 24 40.9 75.5 'Charleston, SC' 38 33.3 80.8 'Nashville, TN' 31 36.7 87.6 'Amarillo, TX' 24 35.6 101.9 'Galveston, TX' 49 29.4 95.5 'Houston, TX' 44 30.1 95.9 'Salt Lake City, UT' 18 41.1 112.3 'Burlington, VT' 7 45 73.9 'Norfolk, VA' 32 37 76.6 'Seattle, WA' 33 48.1 122.5 'Spokane, WA' 19 48.1 117.9 'Madison, WI' 9 43.4 90.2 'Milwaukee, WI' 13 43.3 88.1 'Cheyenne, WY' 14 41.2 104.9
See what is in the workspace.
whos
Name Size Bytes Class Attributes City 56x1 7702 cell DensityEarth 29x1 232 double JanTemp 56x1 448 double Lat 56x1 448 double Long 56x1 448 double UTs_tab 56x4 11232 table all_names 1x2 250 cell ans 1x2 16 double cell_arry2 1x2 272 cell employee 1x1 1474 struct ind 1x0 0 double x 1x2 16 double
Extract the employee's area of interest.
e_area = employee.area;
Display the contents in the window.
e_area
e_area = 'Visualization' 'Inference'
Display Wendy's score.
employee.score(1)
ans = 90
Get MoonJung's area.
employee.area{2}
ans = Inference
Get a partial table by extracting the first
three rows.
U1 = UTs_tab(1:3,:)
U1 = City JanTemp Lat Long ________________ _______ ____ _____ 'Mobile, AL' 44 31.2 88.5 'Montgomery, AL' 38 32.9 86.8 'Phoenix, AZ' 35 33.6 112.5
Get the JanTemp variable.
jt = UTs_tab.JanTemp;
See if it is equal to the JanTemp variable.
isequal(jt,JanTemp)
ans = 1
We get an answer of 1, indicating they are the same.
We can extract the Lat and Long data for the first three cities using the variable names.
U2 = UTs_tab{1:3,{'Lat','Long'}}
U2 = 1.0e+02 * 0.312000000000000 0.885000000000000 0.329000000000000 0.868000000000000 0.336000000000000 1.125000000000000
First load the data back into the workspace.
load iris
Now, we use the semicolon to stack
setosa, versicolor, and virginica data objects.
irisAll = [setosa; versicolor; virginica];
Now look at the workspace to see what is there now.
who
Your variables are: City U1 cell_arry2 jt DensityEarth U2 e_area setosa JanTemp UTs_tab employee versicolor Lat all_names ind virginica Long ans irisAll x
Check on the size of irisAll.
size(irisAll)
ans = 150 4
Load the data if not in the workspace.
load UStemps
Use commas to concatenate as a row.
UStemps = [JanTemp, Lat, Long];
Check the workspace.
who
Your variables are: City U1 ans irisAll x DensityEarth U2 cell_arry2 jt JanTemp UStemps e_area setosa Lat UTs_tab employee versicolor Long all_names ind virginica
Verify the size of UStemps:
size(UStemps)
ans = 56 3
Save the data for the UStemps to a .mat file.
save UStemps City JanTemp Lat Long
That used the command syntax to call a function.
Now use the function syntax.
save('USt.mat','City','JanTemp','Lat','Long')
See what is in our directory now.
dir
. Chapter4.m Chapter8.m html .. Chapter5.m Introduction.m setosa.mat Chapter2.m Chapter6.m USt.mat Chapter3.m Chapter7.m UStemps.mat