80 likes | 263 Views
CS 109 C/C++ Programming for Engineers with MATLAB. Administrative Homework due *tonight* — Monday 4/7 @ 9pm Homework due Wednesday 4/9 @ 9pm note correction posted on Piazza to function 5, " VectorStats "… Topics for Today? structure of MATLAB programs file processing in MATLAB.
E N D
CS 109 C/C++ Programming for Engineers with MATLAB • Administrative • Homework due *tonight* — Monday 4/7 @ 9pm • Homework due Wednesday 4/9 @ 9pm • note correction posted on Piazza to function 5, "VectorStats"… • Topics for Today? • structure of MATLAB programs • file processing in MATLAB CS 109 -- 07 April 2014
The structure of MATLAB programs… • Option 1 is no structure at all — work interactively… • Option 2 is "main" script file + 0 or more "function" files… >> A = [20:10:70] >> THR = (200-A)*0.6 >> plot(A, THR) file name must match function name… PlotTHR.m THR.m % % Program to plot Target Heart Rate: % percentage = input('THR percentage? ') ; PlotTHR(20, 70, 10, percentage) ; function PlotTHR(MinAge, MaxAge, AgeStep, Percent) A = [ MinAge:AgeStep:MaxAge] ; THR = (220 – A) * Percent / 100 ; plot(A, THR) ; end
Working with MATLAB… • Createprograms & functionsvia Home tab, New Script… • Runprograms & functionsby typing filename (without .m) • all files live in sub-folder Documents\MATLAB • language is case-sensitive — Thr¹THR • Variables are dynamically-typed • variable declarations not required • type based on assignment • type can change over time >> Thr **ERROR! >> THR THR percentage? >> x = 3 >> whos('x') % => double! >> x = [1, 2, 3, 4, 5, 6] >> x = 'a string' CS 109 -- 07 April 2014
Reading files in MATLAB: • Does file contain just numbers? load() • Does file contain mixed data (strings, numbers)? dataset() • e.g. spreadsheet-like data with names and values • Reading an Excel spreadsheet?xlsread() • Reading some other file format? • image files? imread( ) • other format? google to see if MATLAB supports… • use low-level "C" functions: fopen( ), fscanf( ), fclose( ) >> imread('cake.ppm'); CS 109 -- 07 April 2014
Example: • Analyzing scores from the midterm… 100 59 78 82 6 87 . . . midterm = load('cs109-midterms.txt') cs109-midterms.txt midterm “column vector” CS 109 -- 07 April 2014
>> midterm = load('cs109-midterms.txt'); >> [rows, cols] = size(midterm) rows = 152 cols = 1 >> min(midterm) ans = 6 >> max(midterm) ans = 100 >> mean(midterm) ans = 83.1776 >> median(midterm) ans = 91.5 >> y = sort(midterm) ; % so plot is readable >> plot(y) • Analysis: • Size • Min • Max • Mean • Median • Sort • Plot CS 109 -- 07 April 2014
Lab section last week (week 10): AnalyzeMidterm.m % % Program to analyze midterms: % AnalyzePlotMidterm('cs109-midterms.txt') ; AnalyzePlotMidterm.m function AnalyzePlotMidterm(filename) fprintf('Analyzing Midterms...\n') ; scores = load(filename) ; scores = sort(scores) ; Min = scores(1) ; Max = scores(end) ; Avg = mean(scores) ; fprintf('Max score: %f\n', Max) ; fprintf('Min score: %f\n', Min) ; fprintf('Avg score: %f\n', Avg) ; plot(scores) ; title('CS109 Midterms Spring/2014') ; end
Working with vectors: >> midterm = load('cs109-midterms.txt') >> first = midterm(1) >> % extract a subset: >> first5 = midterm(1:5) >>% find locations where score < 60: >> failing = midterm < 60 >> % extract failing scores: >> low = midterm( failing ) 0 means false, 1 means true