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

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

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lesson 2: Basic Output You’ve got data……now what???

Lesson 2: Basic Output

You’ve got data……now what???

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

Purpose of lesson 2

• Creating input and output pathways

• Designating an input file

• Creating an output file

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

Review from lesson 1

• What is a:

o Integer?o Double?o Character?o String?o Array?o Cell?

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

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;

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

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/

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

Declaring File Name and Path

%Name of the Fileinfilename = 'L2_data.csv'; %Path to File inpath = 'C:\Documents and Settings\Sandy\Desktop\Classwork\' ;

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

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

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

Let’s open the file

%Name of the Fileinfilename = 'L2_data.csv'; %Path to File inpath = 'C:\Desktop\Classwork\' ;  %Open the file for editingr = csvread([infilename,inpath],1,0);

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

Were you successful?

Try again  %adjust so you only look at columns with numbersr = csvread([infilename,inpath],1,1);

(in lesson 3 we will show you textscan so you can read columns with strings in them

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

Preparing an Output Location

% Outpath needs to exist, not necessarily outfilename

outpath='/Desktop/classwork/L2_data/';

outfilename=[date,'firstoutput.csv'];

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

Error protection

Tiny checks to protect against error.  %Check to make sure outpath exists

if exist(outpath)==0    mkdir(outpath);end

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

Preparing for Output

%Open [outpath,outfile] for writing

out_fid = fopen([outpath,outfilename], 'w+');

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

Wow!

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

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

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

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

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]

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

'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.

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

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 writtenCommas indicate new columns\n indicates a new row

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

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= stringd= decimal f=fixed point decimal

Look these up by typing “ help fprintf”

Values to go in spaces

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

Printing Data to a File

%Outputting data from variables. fprintf(out_fid, '%s,%s,%s\n', subj_num,group,AP_RMS,); 

Declare your variables

subj_num=‘a1’;

Group=‘3’;

AP=r(:,2);

AP_RMS=std(AP);

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

'\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,); 

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

Output per data setOne 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 1of the AP_RMS array

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

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.

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

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