Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 2: Basic Output You’ve got data……now what???

Similar presentations


Presentation on theme: "Lesson 2: Basic Output You’ve got data……now what???"— Presentation transcript:

1 Lesson 2: Basic Output You’ve got data……now what???

2 Purpose of lesson 2 Creating input and output pathways Designating an input file Creating an output file

3 Review from lesson 1 What is a: o Integer? o Double? o Character? o String? o Array? o Cell?

4 First Step - Writing your first code Using the Matlab Editor Comments %This is a comment This is code Testing code in the Matlab Command Window Declaring a variable x = 1;

5 Opening a file from Matlab What you need: Name of the File Path to the File Filename: data.csv collection.txt subject117.tsv File Path: On Windows: C:\My Documents\Collection\ On Mac/Unix: /Users/default/Collection/

6 Declaring File Name and Path %Name of the File filename = 'L1_data.csv'; %Path to File filepath = 'C:\Desktop\Classwork\' ;

7 Inpath is a variable that describes the location of the data Inpath = ['/Desktop/classwork/',subj_num,'/']; Declaring a variable String associated with location of file Previously declared variable Folder designation? [ ] indicates that you have created a new string with everything included

8 Let’s open the file %Name of the File filename = 'L2_data.csv'; %Path to File filepath = 'C:\Desktop\Classwork\' ; %Open the file for editing datafile = fopen([filename,filepath],'r');

9 Were you successful?

10 Preparing an Output Location % Outpath needs to exist, not necessarily outfilename outpath='/Desktop/Matlab/'; outfilename=[date,'firstoutput.csv'];

11 Error protection Tiny checks to protect against error. %Check to make outpath exists if exist(outpath)==0 mkdir(outpath); end

12 Preparing for Output %Open [outpath,outfile] for writing out_fid = fopen([outpath,outfilename], 'w+');

13 Wow! That was a lot of new stuff all at once, wasn't it?

14 Opening a file for output out_fid = fopen([outpath,outfilename], 'w+'); fid is a scalar MATLAB® integer, called a file identifier. You use the fid as the first argument to other file input/output routines. Function “open” String showing location of file File permission

15 Array Concatenation: [A,B] (what can you put into a string?) Concatenation: The act of putting two things together. [A,B] = Concatenation A and B into an Array Try: w = [1,2] x = ['Subject','Condition'] y = ['RT: ', 223] z = ['Perturb',true]

16 'w+'? fopen permissions that you care about: (if in doubt look these up by searching MATLAB help) “help fopen” r = Open a file for reading. w = Open or create a file for writing, discard contents. a = Open or create a file and append data to it. Update mode (+) allows simultaneous reading and writing.

17 Printing Data to a File %Outputting just a String, useful for Headers. fprintf(out_fid,'subj_num,group,AP_RMS,\n'); MATLAB function which specifies writing information to a “device” Specifies the object (location) for writing the information Gives the string that is to be written Commas indicate new columns \n indicates a new row

18 Printing Data to a File %Outputting just a String, useful for Headers. fprintf(out_fid,'subj_num,group,AP_RMS,\n'); %Outputting data from variables. fprintf(out_fid, '%s,%s,%s\n', subj_num,group,AP_RMS,); Format specifier for how information is to be written, s= string d= decimal f=fixed point decimal Look these up by typing “ help fprintf” Values to go in spaces

19 '\n'? Special Characters you will likely use: \n = The newline character \t = The tab character \\ = The backslash character fprintf(out_fid, '%s,%s,%s\n', subj_num,group,AP_RMS,); fprintf(out_fid, '%s\t%s\t%s\n', subj_num,group,AP_RMS,);

20 Output per data set One line vs. multiple lines fprintf(out_fid, '%s,%s,/n', subj_num,AP_RMS,); fprintf(out_fid, '%s,%s,/n', subj_num,AP_RMS(i),); fprintf(out_fid, '%s,%s,/n', subj_num,AP_RMS(1,1),); Single variable output variable for current row (i) variable from row 1, column 1 of the AP_RMS array

21 Practice opening files Open file from class or open one of your own data files Keep records of the error messages Bring a record of errors to class Bring solution if you found it IF you did NOT find solution please send the error to Wayne and Sandy before 10 am Monday morning.

22 Practice making errors Counting is critical for output files Play with some common things that create weirdness in your output file 1) More headers than variables 2) Less headers than variables 3) More % s (or d or f) than variables 4) Put less %s than variables 5) Take the /n off the end of fprintf on data file


Download ppt "Lesson 2: Basic Output You’ve got data……now what???"

Similar presentations


Ads by Google