Review Exercise: Build a 6x6 matrix with 1:6 in each row. You have 36 elements in this array; exract every third element and save into a row vector (use the indexing that we learned last time).
4) FILE I/O
>> save data.mat
>> save data.mat a b
>> load data.mat
>> load data.txt -ascii
Download in_class_datasheet.xls
>> filename = ls('*eet.xls');
>> [num,txt,raw] = xlsread(filename);
>> help csvread
>> help textread
>> help fscanf
Saves all the data in your workspace; .mat is a binary file
Saves the variables a and b into data.mat
Loads the contents of data.mat into your workspace
Loads a text file, if that text file has only numbers and no missing values.
You can get the names of files and save them as variables - this helps automate.
xlsread.m is a fairly useful way to get data into Matlab.
These are all alternative ways to acquire data; each has their strengths and weaknesses.
5) LOGICAL OPERATIONS
Logical operations return a 0 (false) or 1 (true). One can compare a scalar element-wise to any vector or matrix. for example 1 == [1 2 3; 4 5 6] returns a 2x3 of 1's an 0's.
>> a=[1 0 1 0]
>> b=[1 1 0 0]
>> a==b
>> a<=b
>> ~a
>> a&b
>> a & ~a
>> a | b
>> a | ~a
elements in a equal to b.
less than or equal to
not a
a and b
a and not a - always empty
a or b
a or not a - always true
6) SCRIPTS AND FUNCTIONS
FUNCTIONS
A function is a special type of script which receives input variables, operates on them inside a black box and then returns output variables. Functions are a good way to program because they do not modify the input variables in the workspace. Once a function is built, it can be recycled as much as you want in many different pieces of code.
The first line in a function is the function declaration;
function [Out1,Out2,Outn] = myfunction(Input1,Input2);
The above line declares we are making a function. The function will receive two inputs, operate on them in a black box and produce 3 outputs.
Jim = myfunction(I1, I2);
[Jim Pat] = myfunction(I1,I2);
You don't need to take all the ouputs, in this case, Out1 is saved to Jim.
Same as above, but if you take more than one output, the result must be in hard brackets. This is how the function is called from the command line, or inside a script.
>> edity pyth.m
function [r] = pyth(dx,dy);
r = sqrt(dx.^2+dy.^2);
return
>> R = pyth(a,b);
Let's make a function from the code for they hypotenuse that we used in the first introduction.
These are the lines inside the function. We declare the function.
We do the operations
we use 'return' to indicate the end of the function
Now, try out the function.
SCRIPTS
A script is a file with a list of operations to execute. It goes sequentially from top to bottom and each operation is terminated by a carriage return or by the semicolon ';'. In matlab a script name ends in '.m'
>> edit getPlotData.m
>> 'up arrow'
>> plot(num(:,4),num(:,5),'r');
>> prob = unique(num(:,1);
>> c = ['r' 'g' 'b'];
>> for i = 1:length(prob),
>> in = num(:,1)==prob(i);
plot(num(in,4),num(in,5),c(i));
>> end
The 'edit' command will open the Matlab editor - nice colors!, makes coding easier. If getPlotData.m doesn't exist on your path, the editor assumes its a new file.
Use the command history to get and copy our commands for loading in_class_datasheet.xls.
plot RH as a fn. of temperature use a red line (help plot to look at symbol and color choices).
Now, lets plot each probe as a seperate line.
(1) use unique to get the probe numbers from column 1.
(2) make a text vector with plot colors in it
(3) begin for loop,
(4)use the equality to make an indexing array for each probe
(5) use that index to subsample the RH and T vectors for plotting
7) PLOTTING AND GRAPHICS
>> xlabel('Temp. (^{\circ}C)');
>> ylabel('RH %');
>> get(gca);
Label the x-axis. NOTE: Matlab will interepret many LaTeX commands.
Label the y-axis.
Let's check out the axis properties. gca is a handle to the current axis. gcf is a handle to the current figure.