120 likes | 231 Views
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 =
E N D
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 = 30 35 40 45 50 60 65 >> mpg = xydata(:,2)' mpg = 37.1000 36.7000 35.4000 33.4000 30.4000 22.1000 16.7000 Engr 0012 (04-1) LecNotes 09-01
response is assigned as a string to variable file_name 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 Engr 0012 (04-1) LecNotes 09-02
contents of file assigned to designated variable name 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 Engr 0012 (04-1) LecNotes 09-03
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) LecNotes 09-04
comparing two forms of load command file_name is a string (variable) that points to the file filename.dat is the name of a file in the current workspace file contents assigned to designated variable (xydata) file contents assigned to a new variable called filename “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 09-05
x-variable y-variable symbol type displaying data in a graph >> plot(speed,mpg,'r*') Engr 0012 (04-1) LecNotes 09-06
displaying data in a graph 'r*' Engr 0012 (04-1) LecNotes 09-07
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 09-08
annotating your graph not linear, i.e., not y = mx+b title y-axis label x-axis label Engr 0012 (04-1) LecNotes 09-09
searching for a straight line >> semilogy(speed,mpg,'*r') note change in y-scale Engr 0012 (04-1) LecNotes 09-10
searching for a straight line >> loglog(speed,mpg,'*r') note change in both axis scales Engr 0012 (04-1) LecNotes 09-11
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 Engr 0012 (04-1) LecNotes 09-12