Professional Documents
Culture Documents
Command Window
See error messages and results
Workspace
(See variables: names and values)
Command history
(what was done)
Current folder
Working directory
A simple expression
%This file is named somethinguseful.m %Multiply and add two variables x=5+5; y=5*5 %The End ______________________________________________________________________
>> somethinguseful y = 25
Intro to Functions
function [Output1, OutputM]=usefulfunction(Input1, InputN) %This function calls on N inputs and returns M outputs (comment below 1st line) Output1=Input1*InputN; OutputM=Input1+InputN; end %The Real End ______________________________________________________________________
Y = 10
A Useful Function
function [Temp]=Atmosphere(Altitude) %Calculate the Standard atmosphere Temp. for input altitude ReferenceAltitude SurfaceTemperature T_IsoTherm LapseRate = = = = 0; 288.16; 216.66; -0.0065; %[m] SeaLevel (S.L.), h = 0 m %[K] Standard Temp. at S.L. (from Anderson) %[K] Temp. of 1st Isotherm (11-25km) %[K/m] Lapse Rate for gradient region (0-11km)
%[m]
Plot styles
Linear: plot(x,y) Log-Linear: semilogy(x,y) or semilogx(x,y) Log-Log: loglog(x,y) Get Help! :
Examples help plot doc plot help Issue
%Plot Temperature! figure(1); plot(T,h,'k-') xlabel('T [K]'); ylabel('h [m]'); % axis([XMIN XMAX YMIN YMAX]) %Set Custom Axis Limits
saveas(gcf, 'Myplot.png');
The result!
Advanced basics
Import external data (e.g., from a lab) Automate the analysis process Search directory for data files
dir
Pre-Processing Data
Create logical/meaningful naming scheme Should have done at time of acquisition Allows use of dir command to find all files with certain text in filename
Example: 6 files
3 sin waves (sin 1x.txt, sin 2x.txt, sin 3x.txt) 3 cos waves (cos 1x.txt, cos 2x.txt, cos 3x.txt)
sin 1x
1 0.8 0.6 0.4 0.2
f(x)
Loading Data
Loading Data
>> sin_files.name >> sin_files sin_files = 3x1 struct array with fields: name date bytes isdir datenum ans = sin 1x.txt
USER NAME: COMMENTS: sin x Sine Wave START DATA Time 0.000000000 0.000000000 0.100000000 0.099833417 0.200000000 0.198669331
10th line
USER NAME: COMMENTS: cos x cos function Example Text START DATA Time 0.000000000 1.000000000 0.100000000 0.995004165 0.200000000 0.980066578
11th line
Loading Data
Load data from structure arrays using dlmread
(xlsread or csvread would also work) dlmread (filename, delimiter, row, column)
for ii = 1:length(sin_files) data(ii).sin = dlmread([data_folder,sin_files(ii).name],... '',9,0); data(ii).cos = dlmread([data_folder,cos_files(ii).name],... '',10,0); end
Loading Data
Plots of individual data sets
for jj = 1:length(sin_files) figure(jj) hold on plot(data(jj).sin(:,1),data(jj).sin(:,2)) title(['sin ',num2str(jj),'x']); hold off end for jj = 1:length(cos_files) figure(jj+3) hold on plot(data(jj).cos(:,1),data(jj).cos(:,2)) title(['cos ',num2str(jj),'x']); hold off end
Loading Data
Mega plot o data
figure(7) hold on for ii = 1:length(sin_files) subplot(2,3,ii) plot(data(ii).sin(:,1),data(ii).sin(:,2)) title(['sin ',num2str(ii),'x']); subplot(2,3,ii+3) plot(data(ii).cos(:,1),data(ii).cos(:,2)) title(['cos ',num2str(ii),'x']); end hold off
sin 1x
1 0.5
f(x) f(x)
sin 2x
1 0.5 0 -0.5
f(x)
sin 3x
1 0.5 0 -0.5
0 -0.5 -1 0 10 x 20
-1 0
10 x
20
-1 0
10 x
20
cos 1x
1 0.5
f(x) f(x)
cos 2x
1 0.5 0 -0.5
f(x)
cos 3x
1 0.5 0 -0.5
0 -0.5 -1 0 10 x 20
-1 0
10 x
20
-1 0
10 x
20