1 / 8

Administrative Homework due *tonight* — Monday 4/7 @ 9pm Homework due Wednesday 4/9 @ 9pm

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.

hiero
Download Presentation

Administrative Homework due *tonight* — Monday 4/7 @ 9pm Homework due Wednesday 4/9 @ 9pm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. >> 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

  7. 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

  8. 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

More Related