Engr 0012 (04-1) LecNotes loading data from files >> file_name = input( 'Enter data file name ==> ','s') Enter data file name ==> c:\temp\speed_mpg.dat file_name = c:\temp\speed_mpg.dat >> xydata = load(file_name) xydata = (contents of data file displayed) >> speed = xydata(:,1)' speed = >> mpg = xydata(:,2)' mpg =
Engr 0012 (04-1) LecNotes loading data from files >> file_name = input( 'Enter data file name ==> ','s') Enter data file name ==> c:\temp\speed_mpg.dat file_name = c:\temp\speed_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) LecNotes 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) LecNotes loading data from files >> speed = xydata(:,1)' speed = extracting x-y vectors and transposing in same step
Engr 0012 (04-1) LecNotes comparing two forms of load command load filename.dat xydata = load(file_name) filename.dat is the name of a file in the current workspace file_name is a string (variable) that points to the file file contents assigned to a new variable called filename file contents assigned to designated variable ( xydata ) “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) LecNotes displaying data in a graph >> plot(speed,mpg,'r*') x-variable y-variable symbol type
Engr 0012 (04-1) LecNotes displaying data in a graph 'r*'
Engr 0012 (04-1) LecNotes 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) LecNotes annotating your graph x-axis label y-axis label title not linear, i.e., not y = mx+b
Engr 0012 (04-1) LecNotes searching for a straight line >> semilogy(speed,mpg,'*r') note change in y-scale
Engr 0012 (04-1) LecNotes searching for a straight line >> loglog(speed,mpg,'*r') note change in both axis scales
Engr 0012 (04-1) LecNotes recap on data analysis to date 1. function to load data, extract x & y components, get indep var name, dep 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