You are on page 1of 25

Powerful computational tool Data Analysis and Presentation Functions, Algorithms

Choose your destiny.

Logical and efficient layout for debugging and production


Editor
Write all scripts here as you go

Command Window
See error messages and results

Workspace
(See variables: names and values)

Command history
(what was done)

Current folder
Working directory

Most important check box ever!

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

To run script (*or press F5)

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 ______________________________________________________________________

>> [X Y]= usefulfunction(5,5) X = 25

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)

if Altitude <= 11000 %[m] Temp = SurfaceTemperature - LapseRate*(Altitude - ReferenceAltitude);

elseif Altitude >11000


Temp = T_IsoTherm; end %end if statement end %function end

%[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

In general, no book or inter-webs required.

m-file that calls a function


%TemperaturePlot.m %Plot the temperature between 0 and 25,000 m h=0:100:25000; %[m] altitude %Get Temperature using Standard Atmosphere function (Atmosphere.m) for i = 1:length(h) T(i) = Atmosphere(h(i)); end
%Write data to a an XL file (note, MACs write csv files by default) xlswrite('MyTable.xls', [h' T']);

%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!

$1500+ aluminum cases


XLSWRITE with Macs only
>> Warning: Could not start Excel server for export. XLSWRITE will attempt to write file in CSV format.

Writes a csv file instead Import data into Excel


Navigate to the Data Tab
Select get External Data Choose Comma Separated Delimiter

Advanced basics
Import external data (e.g., from a lab) Automate the analysis process Search directory for data files
dir

Load data to the workspace


dlmread, csvread, xlsread, etc.

Use for loop for redundant processing and plotting


Useful for large quantities of data

Two types of plots


One plot to rule them all! (subplot) One plot for each data set

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

Count number of lines in header


Remove any text that comes after data e.g., END DATA on the last line will cause Matlab pain

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)

0 -0.2 -0.4 -0.6 -0.8 -1 0 2 4 6 8 10 x 12 14 16 18 20

Loading Data

Create character string for data directory


data_folder = 'C:\Users\User\Documents\MATLAB\AME 261\Discussion 1\Data\';

Find data files and assign filenames to a structure using dir


sin_files = dir([data_folder,'sin*.txt']); cos_files = dir([data_folder,'cos*.txt']);

View files in structure by typing sin_files.name

Loading Data
>> sin_files.name >> sin_files sin_files = 3x1 struct array with fields: name date bytes isdir datenum ans = sin 1x.txt

ans = sin 2x.txt

ans = sin 3x.txt

Dealing with Headers


National Instruments VirtualBench-Scope V1.00 Report Date Created: 11/15/2012 3:15:11 PM

USER NAME: COMMENTS: sin x Sine Wave START DATA Time 0.000000000 0.000000000 0.100000000 0.099833417 0.200000000 0.198669331

10th line

All Headers are not created equally


National Instruments VirtualBench-Scope V1.00 Report Date Created: 11/15/2012 3:15:11 PM

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

You might also like