Engr/Math/Physics 25 Chp3 MATLAB Functions: Part4 Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Learning Goals Understand the difference Built-In and User-Defined MATLAB Functions Write User Defined Functions Describe Global and Local Variables When to use SUBfunctions as opposed to NESTED-Functions Import Data from an External Data-File • As generated, for example, by an Electronic Data-Acquisition System Engineering/Math/Physics 25: Computational Methods 2 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Private Functions Private functions reside in subdirectories with the special name private, and they are visible only to fcns in the parent directory Assume the directory wmays is on the MATLAB search path. A subdirectory of wmays called private may contain functions that only the functions in wmays can call. Because private functions are invisible outside the parent directory wmays, they can use the same names as functions in other directories. Engineering/Math/Physics 25: Computational Methods 3 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Data Acquisition At Some Point in His/Her Career Virtually EVERY Engineer will need to ACQUIRE and ANALYZE data from some PHYSICAL system The Acquisition of large amounts of Digital Data used to be quite difficult, but today modern Electronics makes this MUCH easier. Main Steps in the design of a Data Acq Sys • Determine WHAT Data to Collect (can be HARD) • Obtain electronic measurement SENSORS • Obtain Sensor↔Computer INTERFACE Hardware & Software to collect data in digital form Engineering/Math/Physics 25: Computational Methods 4 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Typical Data Acq Sys. Items & → Sensor; Sensor: Power & Signal-Conditioning Engineering/Math/Physics 25: Computational Methods 5 Item → USB or Serial Connection Item → Interface Software Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Design: Data-inputs, Sensors Qty Description/Location Type Pwr Src 02 Immersed in coolant stream at Supply & Return connections of chiller Temp K TC n/a [Strawberry Tree Data Acq] [laptop] Need to install 3/8-Tu to 1/4FPT branch TEE in chiller plumbing to add 083886 & 080144. TEE = SS-600-3TTF 13 Temp array around the new TMB bubbler Temp K TC n/a [Strawberry Tree Data Acq] [laptop] Order 2nd DATA shuttle. See item 4932. See bubtcloc.dwg, or prs39602 03 Collant flow leading into the three bubblers Fluid Flw Turbine meter 12 Vdc from NOSHOK controllers [modifed 973680 V-divider card] [Strawberry Tree Data Acq] [laptop] 3 wires: +12Vdc from NoShok, com from NoShok, sq-wave pulse to NoShok 03 MFM on output of bubblers to measure the Qv,B+QN2 mixture output of bubblers 0-5 Vdc Output flow WJ-1000 system [Strawberry Tree Data Acq] [laptop] Use Tylan MFC/MFM extension cards, pn 3780-280 02 In & Out signal on ch2&3 TMB bubbler Hi-res card channel. Card = 80 CARD. Vin = TP8, Vout = TP16 0-5 Vdc Output -30 °C +70 °C WJ-1000 system [TP8 & TP16] [Strawberry Tree Data Acq] [laptop] See item 5270 for Strawberry Tree over-voltage calcs OK Use Tylan MFC/MFM extension cards, pn 3780-280 01 Absolute Barometric Pressure. Locate Setra270 sensor on top of system 0-5 Vdc Output 800 1100 mBar PA 24Vdc, bench top pwr supply from FPD handler project [Strawberry Tree Data Acq] [laptop] Bench test req’d for "088603-000 xdcr,press,0100 psig,atm (model 270)" 24 V pwr supply also power the NoShok controllers Engineering/Math/Physics 25: Computational Methods 6 Output To Notes & Comments Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Sensor Placement Design Engineering/Math/Physics 25: Computational Methods 7 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Computer DataAcq DataFlow Board 1:1194 Write 1194a Bub TCs 0-5V -> Torr Atm Press Computer Interface Cards Ch2 &3 Tsens Write1194b Ch2&3 Thires Board 2:1159 Write 1159a Write1159b TEOS Line TC Chiller TCs Board 3: 160 Write 160a “soft” Charts Bub N2 MFCs MFMs TP1: V -> C Ch2&3 Bub T Write160b TP9: V->C Engineering/Math/Physics 25: Computational Methods 8 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Data Files Most DataAcq Systems record the data using characters listed in the American Standard Code for Information Interchange (ASCII) • The Base ASCII set contains 128 characters (hex 7F) • The Extended set contains 256 characters (hex FF) Engineering/Math/Physics 25: Computational Methods 9 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Data Files cont Data Files Usually have File Extensions of .dat or .txt • Many others exist – in Feb10 found 3737 Different Data Files types on http://www.webopedia.com/quick_ref/fileextensionsfull.asp Windows Wordpad.exe creates ASCII text, and is typically used to EDIT Data Files • Or in MSWord you can save/read files as ‘text only’ – Change the DataFile extension to “.txt” if needed Many Times Data Files Have “Headers” that need to be stripped OUT to Expose data-only Engineering/Math/Physics 25: Computational Methods 10 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Data File Examples KLA-T “KLARFF file fragment DEFECTID XREL YREL XINDEX YINDEX XSIZE YSIZE DEFECTAREA DSIZE CLASSNUMBER TEST IMAGECOUNT ; DefectList 1 2.7076600000e+02 3.6191450000e+03 -4 1 0.249000 0.249000 0.032000 1.7800000000e-01 0 1 0 2 1.5445400000e+02 2.5059200000e+03 -4 -2 0.249000 0.249000 0.027500 1.6500000000e-01 0 1 0 3 1.8203200000e+02 2.3367030000e+03 -4 -3 0.499000 0.499000 0.046800 2.1600000000e-01 0 1 0 4 3.8058700000e+02 2.0016480000e+03 -4 -3 0.249000 0.998000 0.046800 2.1600000000e-01 0 1 0 5 9.2548000000e+01 4.8907490000e+03 -4 -4 0.249000 0.249000 0.032000 1.7800000000e-01 0 1 0 • SPACES Separate the Data FIELDS StrawBerry Tree DasyLab-Software file WorkBench PC for Windows - V 2.60.00 WORKSHEET : 960411D3 Recording Date : 4/11/96, 15:28:28 Block Length : 8 Delta : 0.500000 sec. Number of Channels : 4 Date;Time;TC14: TEOS Line (C);TC16: TEOS Line (C);TC17: Chllr Sup (C);TC18: Chllr Ret (C); 4/11/96;15:28:28.0;74.908;86.706;-22.458;-19.720; 4/11/96;15:28:28.5;74.938;86.716;-22.467;-19.716; 4/11/96;15:28:29.0;74.952;86.705;-22.499;-19.706; 4/11/96;15:28:29.5;74.980;86.702;-22.509;-19.695; • SEMICOLONS Separate the Data FIELDS Engineering/Math/Physics 25: Computational Methods 11 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Data File Examples FLUKE ScopeMeter .csv Data File Fragment ","Input A", ",1, ","Envelope", ",13/32/94, ",19:34:51, ",5.0E-03, ",-1.00E-02, ",2.500000E+01, ",254, ","s", ","5 ms/Div", ",2.00E+00, ",6.32E+00, ",2.500000E+01, ",256, ","V", ","V", -1.00E-02,5.6E-01,6.4E-01 -9.8E-03,4.0E-01,5.6E-01 -9.6E-03,4.8E-01,5.6E-01 -9.4E-03,4.0E-01,5.6E-01 -9.2E-03,4.0E-01,5.6E-01 -9.0E-03,4.0E-01,5.6E-01 Test-Point-3 SCOPEMETER DEMO BOARD 4022 245 0422.2 9.0 Signal (Vdc) 8.0 7.0 ScopeMeter Signal (Vdc) "Title "ID "Type "Date "Time "X Scale "X At 0% "X Resolution "X Size "X Unit "X Label "Y Scale "Y At 50% "Y Resolution "Y Size "Y Unit "Y Label 6.0 5.0 4.0 3.0 2.0 1.0 0.0 -0.01 0.01 0.02 0.03 Time (s). 0.04 file = SM_Demo_Board_0210.xls DataFile HEADER COMMAS Separate Data Engineering/Math/Physics 25: Computational Methods 12 0.00 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Import Data into MATLAB Once the DataAcq System Generates reams of Digital Data, the Engineer or Scientist needs to ANALYZE it To discern its MEANING MATLAB has Extremely Powerful Data-Analysis Tools, but we need to get the data INTO MATLAB first MATLAB provides a Windows-type “wizard” to IMPORT the data; bringing data-file values into the MATLAB Environment Engineering/Math/Physics 25: Computational Methods 13 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt MATLAB: Data Import/Analysis Recall the Fluke ScopeMeter .csv file We Want to import the Data from This File and plot it it Using MATLAB Cables To/From AcceleroMeter Omega ACC-PS1 AcceleroMeter Power Supply ScopeMeter used for Voltage-Level Data Acquisition Fluke-199 200 MHz ScopeMeter Engineering/Math/Physics 25: Computational Methods 14 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Engineering/Math/Physics 25: Computational Methods 15 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Data Acq from H2 Fuel Cell MotorCycle (H2 @ 5000 psig) Engineering/Math/Physics 25: Computational Methods 16 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Engineering/Math/Physics 25: Computational Methods 17 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt All Done for Today Fluke ScopeMeter Engineering/Math/Physics 25: Computational Methods 18 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Engr/Math/Physics 25 Appendix f x 2 x 7 x 9 x 6 3 2 Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engineering/Math/Physics 25: Computational Methods 19 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Import ScopeMeter .csv Data Start the Import Wizard Locate Directory with DataFile Engineering/Math/Physics 25: Computational Methods 20 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Import TP3 .csv Data Find and OPEN DataFile Engineering/Math/Physics 25: Computational Methods 21 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Import TP3 .csv Data MATLAB AutoMatically picks Comma for “Select Column Separator(s)” Engineering/Math/Physics 25: Computational Methods 22 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Import TP3 .csv Data BUT, Data does NOT Preview Properly Suspect HEADER as cause Engineering/Math/Physics 25: Computational Methods 23 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Import TP3 .csv Data Edit Header with MS WordPad HEADER contains NUMBERS – Suspect this is the problem Replace with Pure TEXT Engineering/Math/Physics 25: Computational Methods 24 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Import TP3 .csv Data Give File NEW name to preserve original Form of Data Replace Header w/ 3 lines of Text • All No.s Preceded by Letter(s) Engineering/Math/Physics 25: Computational Methods 25 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Import TP3 .csv Data Use Import Wizard to Open NEW .csv file Preview Looks MUCH better • Even Picked Out Column Headers Engineering/Math/Physics 25: Computational Methods 26 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Import TP3 .csv Data The DATA Preview Looks Really Promising Data Values Arrayed in SpreadSheet-like cell arrangement → click Next> Engineering/Math/Physics 25: Computational Methods 27 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Import TP3 .csv Data Can Chose Variable Format as either: 3-Col ARRAY or 3 individual Col-VECTORS Engineering/Math/Physics 25: Computational Methods 28 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Import TP3 .csv Data Do it the HARD way with 3-Col Array • Change VAR name to meaningful Value MATLAB Array Var named SM_TP3 → click Next> Engineering/Math/Physics 25: Computational Methods 29 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Import TP3 .csv Data Chk that SM_TP3 appears in WorkSpace • SoFar, SoGood... Now Some Physics • At each Sample Time the ScopeMeter Records TWO Data Points during the sampling interval: Max & Min Choose to Plot MAX, which is stored Col-3 • c.f. Column-3 on previous slide Engineering/Math/Physics 25: Computational Methods 30 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Plot TP3 t vs V Data The Command Window Plotting Session >> % make time vector, t, from Array Col-1 >> t = SM_TP3(:,1); >> % construct voltage vector, V, from Array Col-3 >> V = SM_TP3(:,3); >> plot(t,V), xlabel('Time (s)'), ylabel('TP3 E-Potential (volt)'),... title('Fluke ScopeMeter SignGen TestPt3'), grid Engineering/Math/Physics 25: Computational Methods 31 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Plot TP3 t vs V Engineering/Math/Physics 25: Computational Methods 32 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Nested Functions MATLAB permits placement of the definitions of one or more functions within another function. Functions so defined are said to be nested within the main function. Engineering/Math/Physics 25: Computational Methods 33 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Nested Functions cont Like any .m-file function, a nested function contains the usual components of an .m-file function. However, a nested function must always terminate with an end statement. In fact, if an .m-file contains at least one nested function, then all functions, including subfunctions, in the file must terminate with an end statement Engineering/Math/Physics 25: Computational Methods 34 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Example Nested Function This example assigns a function handle for a nested function and then passes the handle to the MATLAB function fminbnd to find the minimum point on a parabola. The ConcaveUp fcn constructs and returns a function handle f for the nested function p. This handle gets passed to fminbnd. function f = ConcaveUp(a, b, c) f = @p; function y = p(x) y = a*x^2 + b*x + c; end end Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 35 BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Example Nested Function cont Test the Nest in the Command Window >> g = ConcaveUp(7, 41, -73); >> fminbnd(g, -50, 50) ans = -2.9286 Note than the function p(x) can see the variables a, b, and c in the calling function’s workspace. Engineering/Math/Physics 25: Computational Methods 36 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Nested vs Sub Functions Nested functions might seem to be the same as subfunctions, but they are not. Nested functions have two unique properties: 1. A nested function can access the workspaces of all functions inside of which it is nested. Thus, – a variable that has a value assigned to it by the primary function can be read or overwritten by a function nested at any level within the main function – A variable assigned in a nested function can be read or overwritten by any of the functions containing that function. Engineering/Math/Physics 25: Computational Methods 37 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt Nested vs Sub Functions cont The second unique property: 2. If you construct a function handle for a nested function, the handle not only stores the information needed to access the nested function; it also stores the values of all variables shared between the nested function and those functions that contain it. – This means that these variables persist in memory between calls made by means of the function handle. Engineering/Math/Physics 25: Computational Methods 38 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Functions-4.ppt