Matlab_2

advertisement
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
Review from lesson 1
• What is a:
o
o
o
o
o
o
Integer?
Double?
Character?
String?
Array?
Cell?
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;
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/
Declaring File Name and Path
%Name of the File
infilename = 'L2_data.csv';
%Path to File
inpath = 'C:\Documents and Settings\Sandy\Desktop\Classwork\' ;
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
Let’s open the file
%Name of the File
infilename = 'L2_data.csv';
%Path to File
inpath = 'C:\Desktop\Classwork\' ;
%Open the file for editing
r = csvread([infilename,inpath],1,0);
Were you successful?
Try again
%adjust so you only look at columns with numbers
r = csvread([infilename,inpath],1,1);
(in lesson 3 we will show you textscan so you can read
columns with strings in them
Preparing an Output Location
% Outpath needs to exist, not necessarily outfilename
outpath='/Desktop/classwork/L2_data/';
outfilename=[date,'firstoutput.csv'];
Error protection
Tiny checks to protect against error.
%Check to make sure outpath exists
if exist(outpath)==0
mkdir(outpath);
end
Preparing for Output
%Open [outpath,outfile] for writing
out_fid = fopen([outpath,outfilename], 'w+');
Wow!
That was a lot of new stuff all at once, wasn't it?
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
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]
'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.
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
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
Values to go in spaces
d= decimal
f=fixed point decimal
Look these up by typing “ help fprintf”
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);
'\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,);
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
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.
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