basic file input and output copyright © software carpentry 2011 this work is licensed under the...
TRANSCRIPT
![Page 1: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/1.jpg)
Basic File Input and Output
Copyright © Software Carpentry 2011
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.
MATLAB Programming
![Page 2: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/2.jpg)
MATLAB Basic I/O
Scientists use (a lot of) data
Images ( matrix )
Sound files ( array or matrix)
Video ( 3 dimension matrix )
Instrument output ( time series )
Spreadsheets ( set of matrices )
…
MATLAB can read all of these… and more.
![Page 3: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/3.jpg)
MATLAB Basic I/O
Data import and output:
Input and convert to data structure
Print output to the screen
Write arrays in a suitable file format.
![Page 4: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/4.jpg)
MATLAB Basic I/O
Two ways to import data:
Using the graphical interface
![Page 5: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/5.jpg)
MATLAB Basic I/O
Two ways to import data:
Using the graphical interface
Easiest to learn
MATLAB figures out what kind of file you have
![Page 6: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/6.jpg)
MATLAB Basic I/O
Two ways to import data:
Using the graphical interface
Easiest to learn
MATLAB figures out what kind of file you have
Using the command line
![Page 7: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/7.jpg)
MATLAB Basic I/O
Two ways to import data:
Using the graphical interface
Easiest to learn
MATLAB figures out what kind of file you have
Using the command line
Can be automated
Supports many more formats
![Page 8: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/8.jpg)
MATLAB Basic I/O
![Page 9: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/9.jpg)
MATLAB Basic I/O
![Page 10: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/10.jpg)
MATLAB Basic I/O
.mat file:
MATLAB’s prefered format.
Binary, which saves space.
Designed to store multiple variables.
![Page 11: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/11.jpg)
MATLAB Basic I/O
Double click
![Page 12: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/12.jpg)
MATLAB Basic I/O
Text data: comma or space separated
My file: tweets.txt
Latitude longitude tweet_density
43.650332 -79.389013 0.000739
43.669778 -79.38267 0.00273
43.648943 -79.391319 0.003818
![Page 13: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/13.jpg)
MATLAB Basic I/O
Load data into MATLAB:
>> data = importdata(‘tweets.txt’);
![Page 14: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/14.jpg)
MATLAB Basic I/O
Load data into MATLAB:
>> data = importdata(‘tweets.txt’);
importdata is a ‘wrapper function.’
Identifies the format of the file
Is it text, image, MAT file format, spreadsheet, sound, XML, … ?
Find the correct function to load the file.
Optional arguments can help figure this out.
![Page 15: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/15.jpg)
MATLAB Basic I/O
importdata(file, options)
load(…) xmlread(…) … imread(…)
![Page 16: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/16.jpg)
MATLAB Basic I/O
>> in = importdata(‘tweets.txt’);
>> inin =
data: [40000x3 double]
textdata: {'Lat' ' Lng' ' Weight'}
colheaders: {'Lat' ' Lng' ' Weight'}
![Page 17: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/17.jpg)
MATLAB Basic I/O
>> in = importdata(‘tweets.txt’);
>> inin =
data: [40000x3 double]
textdata: {'Lat' ' Lng' ' Weight'}
colheaders: {'Lat' ' Lng' ' Weight'}
![Page 18: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/18.jpg)
MATLAB Basic I/O
>> in = importdata(‘tweets.txt’);
>> inin =
data: [40000x3 double]
textdata: {'Lat' ' Lng' ' Weight'}
colheaders: {'Lat' ' Lng' ' Weight'}
![Page 19: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/19.jpg)
MATLAB Basic I/O
>> in = importdata(‘tweets.txt’);
>> inin =
data: [40000x3 double]
textdata: {'Lat' ' Lng' ' Weight'}
colheaders: {'Lat' ' Lng' ' Weight'}
![Page 20: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/20.jpg)
MATLAB Basic I/O
MATLAB can also read things like spreadsheets:
>> in = importdata(‘mydata.xls’);
>> in
in =
Sheet1 = [6 x 2 double]
Sheet2 = [13 x 1 double]
![Page 21: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/21.jpg)
MATLAB Basic I/O
MATLAB can also read things like spreadsheets:
>> in = importdata(‘mydata.xls’);
>> in
in =
Sheet1 = [6 x 2 double]
Sheet2 = [13 x 1 double]
Each sheet is a member of the structure
![Page 22: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/22.jpg)
MATLAB Basic I/O
Exporting results:
Visualization
Printing to screen
Exporting to a file
![Page 23: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/23.jpg)
MATLAB Basic I/O
Exporting results:
Visualization
Printing to screen
Exporting to a file
Special episode on graphing and images
![Page 24: Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See](https://reader031.vdocuments.us/reader031/viewer/2022032607/56649ec55503460f94bd07f5/html5/thumbnails/24.jpg)
February 2011
created by
Richard T. Guy
Copyright © Software Carpentry 2011
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.