loading data from files filename input Enter data

  • Slides: 12
Download presentation
loading data from files >> file_name = input( 'Enter data file name ==> ',

loading data from files >> file_name = input( 'Enter data file name ==> ', 's') Enter data file name ==> c: tempspeed_mpg. dat file_name = c: tempspeed_mpg. dat >> xydata = load(file_name) xydata = (contents of data file displayed) >> speed = xydata(: , 1)' speed = 30 35 40 45 50 60 65 >> mpg = xydata(: , 2)' mpg = 37. 1000 36. 7000 Engr 0012 (04 -1) Lec. Notes 09 -01 35. 4000 33. 4000 30. 4000 22. 1000 16. 7000

loading data from files >> file_name = input( 'Enter data file name ==> ',

loading data from files >> file_name = input( 'Enter data file name ==> ', 's') Enter data file name ==> c: tempspeed_mpg. dat file_name = c: tempspeed_mpg. dat asking user to identify the filename/path where data is kept 's' tells MATLAB the input data will be a string respond with file name and location path can be omitted if file is in current MATLAB directory response is assigned as a string to variable file_name Engr 0012 (04 -1) Lec. Notes 09 -02

loading data from files >> xydata = load(file_name) xydata = (contents of data file

loading data from files >> xydata = load(file_name) xydata = (contents of data file displayed) functional form of load command file_name must be a string identifying where file can be found contents of file assigned to designated variable name Engr 0012 (04 -1) Lec. Notes 09 -03

loading data from files >> speed = xydata(: , 1)' speed = 30 35

loading data from files >> speed = xydata(: , 1)' speed = 30 35 40 45 50 60 65 extracting x-y vectors and transposing in same step Engr 0012 (04 -1) Lec. Notes 09 -04

comparing two forms of load command load filename. dat xydata = load(file_name) is the

comparing two forms of load command load filename. dat xydata = load(file_name) is the name of a file_name is a string (variable) that points to the file in the current workspace file contents assigned to a new file contents assigned to designated variable (xydata) variable called filename. dat “hardwired” - cannot change data file name generic - filename can be changed by asking user good when working directly in MATLAB workspace preferable for use in function to get data from any file Engr 0012 (04 -1) Lec. Notes 09 -05

displaying data in a graph >> plot(speed, mpg, 'r*') x-variable Engr 0012 (04 -1)

displaying data in a graph >> plot(speed, mpg, 'r*') x-variable Engr 0012 (04 -1) Lec. Notes 09 -06 y-variable symbol type

displaying data in a graph Engr 0012 (04 -1) Lec. Notes 09 -07 'r*'

displaying data in a graph Engr 0012 (04 -1) Lec. Notes 09 -07 'r*'

annotating your graph >> xname = input( 'Enter x-axis name ==> ', 's'); Enter

annotating your graph >> xname = input( 'Enter x-axis name ==> ', 's'); Enter x-axis name ==> speed >> yname = input( 'Enter y-axis name ==> ', 's'); Enter y-axis name ==> mpg >> graphname = input( 'Enter graph name ==> ', 's'); Enter graph name ==> MPG versus Speed >> xlabel(xname) >> ylabel(yname) >> title(graphname) Engr 0012 (04 -1) Lec. Notes 09 -08

annotating your graph not linear, i. e. , not y = mx+b title y-axis

annotating your graph not linear, i. e. , not y = mx+b title y-axis label x-axis label Engr 0012 (04 -1) Lec. Notes 09 -09

searching for a straight line >> semilogy(speed, mpg, '*r') note change in y-scale Engr

searching for a straight line >> semilogy(speed, mpg, '*r') note change in y-scale Engr 0012 (04 -1) Lec. Notes 09 -10

searching for a straight line >> loglog(speed, mpg, '*r') note change in both axis

searching for a straight line >> loglog(speed, mpg, '*r') note change in both axis scales Engr 0012 (04 -1) Lec. Notes 09 -11

recap on data analysis to date 1. function to load data, extract x &

recap on data analysis to date 1. function to load data, extract x & y components, get indep var name, data set title, and symbol type needs: results: xpts, ypts, xname, yname, graphname, symbol 2. script that calls get data function, asks user for choice of plot type (linear, semilog, log-log), and displays requested (annotated) plot Engr 0012 (04 -1) Lec. Notes 09 -12