SlideShare ist ein Scribd-Unternehmen logo
1 von 9
Downloaden Sie, um offline zu lesen
Input/Output in
MATLAB
CHEN 1703
See also:
• The class wiki page notes on I/O
• Your text book, §3.4 and Appendix C
1
Thursday, October 9, 2008
VERY Basic MATLAB File I/O
save filename x y -ASCII
• filename is the name of the file that you want to write data to.
• x, y are variables to be written to the file.
‣ If omitted, all variables are written.
• -ASCII tells Matlab to write the data in a format that you can read.
‣ If omitted, data will be written in binary format.
‣ best for large amounds of data
load filename x y
• This is the complimentary command to save.
• Reads variables x and y from file filename
‣ If variables are omitted, all variables are loaded...
Saving variables to files & Loading variables from files
clear;
x = linspace(-pi,pi);
y = cos(x);
save myVariables x y;
clear;
load myVariables;
who; plot(x,y);
2
Thursday, October 9, 2008
Formatted Output in Matlab
disp(x) - prints the contents of variable x.
fprintf(...) - use for formatted printing
• Allows much more control over output
• Syntax: fprintf(‘text & formatting’,variables);
• Text formatting:
‣ %a.bc
‣ a - minimum width of output buffer
‣ b - number of digits past decimal point
‣ c - formatting scheme
‣ f - floating point (typical format) 12.345
‣ e - scientific notation - 1.2345e1
‣ s - string format
x = [1.1 2.2 3.3 4.4];
y = 2*x;
fprintf('Hello. (%1.3f,%1.3f), (%1.1f,%1.0f)n',...
x(1),y(1),x(3),y(3));
Hello. (1.100,2.200), (3.3,7)
Control
Code Description Example
n Begin a new line fprintf('hello.n');
t Insert a “tab” fprintf('thello.n');
 insert a backslash fprintf('hello.n');
'' Insert a single quote fprintf('''hello.''n');
%% Insert a % sign fprintf(‘%%%1.2fn’,95.6);
3
Thursday, October 9, 2008
Formatted Output - Examples
fprintf('%6s%8sn','index','value');
fprintf('--------------n');
n = 5;
a = zeros(5,1);
for( i=1:5 )
a(i) = 2*i+1;
fprintf('%6.0f%8.1fn',i,a(i));
end
Repeat temperature conversion example
using fprintf rather than disp.
4
Thursday, October 9, 2008
File Output in MATLAB
Three steps:
• Open the file
‣ fid = fopen(filename,’w’);
‣ ‘w’ tells matlab that we want to WRITE to
the file.
‣ see “help fopen” for more information.
• Write to the file
‣ fprintf(fid,format,variables);
• Close the file
‣ fclose(fid);
Example:
Temperature conversion
example - write results to a
file called “tempTable.dat”
5
Thursday, October 9, 2008
File Input in MATLAB
Import wizard “File→Import Data”
• Allows you to import data from delimited files (spreadsheets, etc)
Importing “spreadsheet” data
• dlmread - import data from a delimited file (you choose the delimiter)
• xlsread - import data from Excel.
General file input - three steps:
• fid=fopen(filename,’r’) - open a file to allow detailed input control.
‣ ‘r’ tells matlab that we want to READ from the file.
• a=fscanf(fid,format,size);
‣ Works like file writing, but use fscanf rather than fprintf.
‣ fid - file id that you want to read from
‣ format - how you want to save the information (string, number)
‣ ‘%s’ to read a string, ‘%f’ to read a floating point number, ‘%e’ to read scientific
notation.
‣ size - how many entries to read.
‣ feof(fid) - returns true if end of file, false otherwise.
• fclose(fid);
6
Thursday, October 9, 2008
clear; clc;
% open a file to read - first line contains
% the order of the polynomial. Second line
% contains the polynomial coefficients.
fid = fopen('poly.dat',’r’);
% read the order of the polynomial
n = fscanf(fid,'%f',1);
% read all of the polynomial coefficients
a = fscanf(fid,'%f',n+1);
4
1.0 2 0.02 4.0 0
2
0 1 2.3
“poly.dat”
“poly.dat”
File Input - Example 1
p(x) =
4
!
i=0
ai xi
= a0 + a1x + a2x2
+ a3x3
+ a4x4
p(x) =
n
!
i=0
ai xi
General form of an
nth order polynomial:
For a quartic
(n=4) we have:
7
Thursday, October 9, 2008
File Input - Example 2
Have the user specify the number of elements in a molecule in a file. Read
the file and then output the molecular weight. Include H C O N S.
Steps:
1. Set up the MW vector: mw=[mwH mwC mwO mwN mwS];
2. Set up the composition vector: nAtoms=zeros(1,5);
3. Open the file
4. While we aren’t at the end of the file
1. read a line.
2. Assign the proper entry in nAtoms
5. Calculate the mixture molecular weight & output it.
Convention: input lines look like:
Element Number
Example:
C 1
H 4
8
Thursday, October 9, 2008
Excel - I/O
Reading delimited data
• File→Open (may need to select file type to be “all files”)
• An import wizard opens, allowing you to select delimiters
9
Thursday, October 9, 2008

Weitere ähnliche Inhalte

Ähnlich wie IO_Matlab.pdf

Ähnlich wie IO_Matlab.pdf (20)

Programming in C
Programming in CProgramming in C
Programming in C
 
file
filefile
file
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
EST 102 Programming in C-MODULE 5
 EST 102 Programming in C-MODULE 5 EST 102 Programming in C-MODULE 5
EST 102 Programming in C-MODULE 5
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
File handling-c
File handling-cFile handling-c
File handling-c
 
Unit v
Unit vUnit v
Unit v
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
File management
File managementFile management
File management
 
File Management in C
File Management in CFile Management in C
File Management in C
 
7.0 files and c input
7.0 files and c input7.0 files and c input
7.0 files and c input
 
Files nts
Files ntsFiles nts
Files nts
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
 
File handling in c
File handling in c File handling in c
File handling in c
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
File in C language
File in C languageFile in C language
File in C language
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 

Mehr von ssuserd9c0ee1

Mehr von ssuserd9c0ee1 (9)

Share 'Chemical EOR (1).ppt'.ppt
Share 'Chemical EOR (1).ppt'.pptShare 'Chemical EOR (1).ppt'.ppt
Share 'Chemical EOR (1).ppt'.ppt
 
1- introduction to EOR.pptx
1- introduction to EOR.pptx1- introduction to EOR.pptx
1- introduction to EOR.pptx
 
week5.ppt
week5.pptweek5.ppt
week5.ppt
 
Share 'Chemical EOR (1).ppt'.ppt
Share 'Chemical EOR (1).ppt'.pptShare 'Chemical EOR (1).ppt'.ppt
Share 'Chemical EOR (1).ppt'.ppt
 
awq.pdf
awq.pdfawq.pdf
awq.pdf
 
aw.pdf
aw.pdfaw.pdf
aw.pdf
 
asa.pdf
asa.pdfasa.pdf
asa.pdf
 
are.pdf
are.pdfare.pdf
are.pdf
 
12_2022_03_13!08_25_53_PM.ppt
12_2022_03_13!08_25_53_PM.ppt12_2022_03_13!08_25_53_PM.ppt
12_2022_03_13!08_25_53_PM.ppt
 

Kürzlich hochgeladen

Laxmi Nagar Call Girls : ☎ 8527673949, Low rate Call Girls
Laxmi Nagar Call Girls : ☎ 8527673949, Low rate Call GirlsLaxmi Nagar Call Girls : ☎ 8527673949, Low rate Call Girls
Laxmi Nagar Call Girls : ☎ 8527673949, Low rate Call Girlsashishs7044
 
Bobbie goods colorinsssssssssssg book.pdf
Bobbie goods colorinsssssssssssg book.pdfBobbie goods colorinsssssssssssg book.pdf
Bobbie goods colorinsssssssssssg book.pdflunavro0105
 
Faridabad Call Girls : ☎ 8527673949, Low rate Call Girls
Faridabad Call Girls : ☎ 8527673949, Low rate Call GirlsFaridabad Call Girls : ☎ 8527673949, Low rate Call Girls
Faridabad Call Girls : ☎ 8527673949, Low rate Call Girlsashishs7044
 
Russian Call Girls Delhi NCR 9999965857 Call or WhatsApp Anytime
Russian Call Girls Delhi NCR 9999965857 Call or WhatsApp AnytimeRussian Call Girls Delhi NCR 9999965857 Call or WhatsApp Anytime
Russian Call Girls Delhi NCR 9999965857 Call or WhatsApp AnytimeKomal Khan
 
Iffco Chowk Call Girls : ☎ 8527673949, Low rate Call Girls
Iffco Chowk Call Girls : ☎ 8527673949, Low rate Call GirlsIffco Chowk Call Girls : ☎ 8527673949, Low rate Call Girls
Iffco Chowk Call Girls : ☎ 8527673949, Low rate Call Girlsashishs7044
 
Benjamin Portfolio Process Work Slideshow
Benjamin Portfolio Process Work SlideshowBenjamin Portfolio Process Work Slideshow
Benjamin Portfolio Process Work Slideshowssuser971f6c
 
Value Aspiration And Culture Theory of Architecture
Value Aspiration And Culture Theory of ArchitectureValue Aspiration And Culture Theory of Architecture
Value Aspiration And Culture Theory of ArchitectureDarrenMasbate
 
New_Cross_Over (Comedy storyboard sample)
New_Cross_Over (Comedy storyboard sample)New_Cross_Over (Comedy storyboard sample)
New_Cross_Over (Comedy storyboard sample)DavonBrooks
 
Burari Call Girls : ☎ 8527673949, Low rate Call Girls
Burari Call Girls : ☎ 8527673949, Low rate Call GirlsBurari Call Girls : ☎ 8527673949, Low rate Call Girls
Burari Call Girls : ☎ 8527673949, Low rate Call Girlsashishs7044
 
8377087607, Door Step Call Girls In Gaur City (NOIDA) 24/7 Available
8377087607, Door Step Call Girls In Gaur City (NOIDA) 24/7 Available8377087607, Door Step Call Girls In Gaur City (NOIDA) 24/7 Available
8377087607, Door Step Call Girls In Gaur City (NOIDA) 24/7 Availabledollysharma2066
 
Gurgaon Call Girls : ☎ 8527673949, Low rate Call Girls
Gurgaon Call Girls : ☎ 8527673949, Low rate Call GirlsGurgaon Call Girls : ☎ 8527673949, Low rate Call Girls
Gurgaon Call Girls : ☎ 8527673949, Low rate Call Girlsashishs7044
 
Anand Vihar Call Girls : ☎ 8527673949, Low rate Call Girls
Anand Vihar Call Girls : ☎ 8527673949, Low rate Call GirlsAnand Vihar Call Girls : ☎ 8527673949, Low rate Call Girls
Anand Vihar Call Girls : ☎ 8527673949, Low rate Call Girlsashishs7044
 
Dilshad Garden Call Girls : ☎ 8527673949, Low rate Call Girls
Dilshad Garden Call Girls : ☎ 8527673949, Low rate Call GirlsDilshad Garden Call Girls : ☎ 8527673949, Low rate Call Girls
Dilshad Garden Call Girls : ☎ 8527673949, Low rate Call Girlsashishs7044
 
TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...
TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...
TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...door45step
 
Triangle Vinyl Record Store, Clermont Florida
Triangle Vinyl Record Store, Clermont FloridaTriangle Vinyl Record Store, Clermont Florida
Triangle Vinyl Record Store, Clermont FloridaGabrielaMiletti
 
FULL ENJOY - 9953040155 Call Girls in Dwarka Mor | Delhi
FULL ENJOY - 9953040155 Call Girls in Dwarka Mor | DelhiFULL ENJOY - 9953040155 Call Girls in Dwarka Mor | Delhi
FULL ENJOY - 9953040155 Call Girls in Dwarka Mor | DelhiMalviyaNagarCallGirl
 
Karol Bagh Call Girls : ☎ 8527673949, Low rate Call Girls
Karol Bagh Call Girls : ☎ 8527673949, Low rate Call GirlsKarol Bagh Call Girls : ☎ 8527673949, Low rate Call Girls
Karol Bagh Call Girls : ☎ 8527673949, Low rate Call Girlsashishs7044
 
Jlt Call Girl +971509430017 Indian Call Girl in Jlt By Dubai Call Girl
Jlt Call Girl +971509430017 Indian Call Girl in Jlt By Dubai Call GirlJlt Call Girl +971509430017 Indian Call Girl in Jlt By Dubai Call Girl
Jlt Call Girl +971509430017 Indian Call Girl in Jlt By Dubai Call Girllijeho2176
 
Jvc Call Girl +971528604116 Indian Call Girl in Jvc By Dubai Call Girl
Jvc Call Girl +971528604116 Indian Call Girl in Jvc By Dubai Call GirlJvc Call Girl +971528604116 Indian Call Girl in Jvc By Dubai Call Girl
Jvc Call Girl +971528604116 Indian Call Girl in Jvc By Dubai Call Girllijeho2176
 
How Can You Get Dubai Call Girls +971564860409 Call Girls Dubai?
How Can You Get Dubai Call Girls +971564860409 Call Girls Dubai?How Can You Get Dubai Call Girls +971564860409 Call Girls Dubai?
How Can You Get Dubai Call Girls +971564860409 Call Girls Dubai?kexey39068
 

Kürzlich hochgeladen (20)

Laxmi Nagar Call Girls : ☎ 8527673949, Low rate Call Girls
Laxmi Nagar Call Girls : ☎ 8527673949, Low rate Call GirlsLaxmi Nagar Call Girls : ☎ 8527673949, Low rate Call Girls
Laxmi Nagar Call Girls : ☎ 8527673949, Low rate Call Girls
 
Bobbie goods colorinsssssssssssg book.pdf
Bobbie goods colorinsssssssssssg book.pdfBobbie goods colorinsssssssssssg book.pdf
Bobbie goods colorinsssssssssssg book.pdf
 
Faridabad Call Girls : ☎ 8527673949, Low rate Call Girls
Faridabad Call Girls : ☎ 8527673949, Low rate Call GirlsFaridabad Call Girls : ☎ 8527673949, Low rate Call Girls
Faridabad Call Girls : ☎ 8527673949, Low rate Call Girls
 
Russian Call Girls Delhi NCR 9999965857 Call or WhatsApp Anytime
Russian Call Girls Delhi NCR 9999965857 Call or WhatsApp AnytimeRussian Call Girls Delhi NCR 9999965857 Call or WhatsApp Anytime
Russian Call Girls Delhi NCR 9999965857 Call or WhatsApp Anytime
 
Iffco Chowk Call Girls : ☎ 8527673949, Low rate Call Girls
Iffco Chowk Call Girls : ☎ 8527673949, Low rate Call GirlsIffco Chowk Call Girls : ☎ 8527673949, Low rate Call Girls
Iffco Chowk Call Girls : ☎ 8527673949, Low rate Call Girls
 
Benjamin Portfolio Process Work Slideshow
Benjamin Portfolio Process Work SlideshowBenjamin Portfolio Process Work Slideshow
Benjamin Portfolio Process Work Slideshow
 
Value Aspiration And Culture Theory of Architecture
Value Aspiration And Culture Theory of ArchitectureValue Aspiration And Culture Theory of Architecture
Value Aspiration And Culture Theory of Architecture
 
New_Cross_Over (Comedy storyboard sample)
New_Cross_Over (Comedy storyboard sample)New_Cross_Over (Comedy storyboard sample)
New_Cross_Over (Comedy storyboard sample)
 
Burari Call Girls : ☎ 8527673949, Low rate Call Girls
Burari Call Girls : ☎ 8527673949, Low rate Call GirlsBurari Call Girls : ☎ 8527673949, Low rate Call Girls
Burari Call Girls : ☎ 8527673949, Low rate Call Girls
 
8377087607, Door Step Call Girls In Gaur City (NOIDA) 24/7 Available
8377087607, Door Step Call Girls In Gaur City (NOIDA) 24/7 Available8377087607, Door Step Call Girls In Gaur City (NOIDA) 24/7 Available
8377087607, Door Step Call Girls In Gaur City (NOIDA) 24/7 Available
 
Gurgaon Call Girls : ☎ 8527673949, Low rate Call Girls
Gurgaon Call Girls : ☎ 8527673949, Low rate Call GirlsGurgaon Call Girls : ☎ 8527673949, Low rate Call Girls
Gurgaon Call Girls : ☎ 8527673949, Low rate Call Girls
 
Anand Vihar Call Girls : ☎ 8527673949, Low rate Call Girls
Anand Vihar Call Girls : ☎ 8527673949, Low rate Call GirlsAnand Vihar Call Girls : ☎ 8527673949, Low rate Call Girls
Anand Vihar Call Girls : ☎ 8527673949, Low rate Call Girls
 
Dilshad Garden Call Girls : ☎ 8527673949, Low rate Call Girls
Dilshad Garden Call Girls : ☎ 8527673949, Low rate Call GirlsDilshad Garden Call Girls : ☎ 8527673949, Low rate Call Girls
Dilshad Garden Call Girls : ☎ 8527673949, Low rate Call Girls
 
TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...
TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...
TOP BEST Call Girls In SECTOR 62 (Noida) ꧁❤ 8375860717 ❤꧂ Female Escorts Serv...
 
Triangle Vinyl Record Store, Clermont Florida
Triangle Vinyl Record Store, Clermont FloridaTriangle Vinyl Record Store, Clermont Florida
Triangle Vinyl Record Store, Clermont Florida
 
FULL ENJOY - 9953040155 Call Girls in Dwarka Mor | Delhi
FULL ENJOY - 9953040155 Call Girls in Dwarka Mor | DelhiFULL ENJOY - 9953040155 Call Girls in Dwarka Mor | Delhi
FULL ENJOY - 9953040155 Call Girls in Dwarka Mor | Delhi
 
Karol Bagh Call Girls : ☎ 8527673949, Low rate Call Girls
Karol Bagh Call Girls : ☎ 8527673949, Low rate Call GirlsKarol Bagh Call Girls : ☎ 8527673949, Low rate Call Girls
Karol Bagh Call Girls : ☎ 8527673949, Low rate Call Girls
 
Jlt Call Girl +971509430017 Indian Call Girl in Jlt By Dubai Call Girl
Jlt Call Girl +971509430017 Indian Call Girl in Jlt By Dubai Call GirlJlt Call Girl +971509430017 Indian Call Girl in Jlt By Dubai Call Girl
Jlt Call Girl +971509430017 Indian Call Girl in Jlt By Dubai Call Girl
 
Jvc Call Girl +971528604116 Indian Call Girl in Jvc By Dubai Call Girl
Jvc Call Girl +971528604116 Indian Call Girl in Jvc By Dubai Call GirlJvc Call Girl +971528604116 Indian Call Girl in Jvc By Dubai Call Girl
Jvc Call Girl +971528604116 Indian Call Girl in Jvc By Dubai Call Girl
 
How Can You Get Dubai Call Girls +971564860409 Call Girls Dubai?
How Can You Get Dubai Call Girls +971564860409 Call Girls Dubai?How Can You Get Dubai Call Girls +971564860409 Call Girls Dubai?
How Can You Get Dubai Call Girls +971564860409 Call Girls Dubai?
 

IO_Matlab.pdf

  • 1. Input/Output in MATLAB CHEN 1703 See also: • The class wiki page notes on I/O • Your text book, §3.4 and Appendix C 1 Thursday, October 9, 2008
  • 2. VERY Basic MATLAB File I/O save filename x y -ASCII • filename is the name of the file that you want to write data to. • x, y are variables to be written to the file. ‣ If omitted, all variables are written. • -ASCII tells Matlab to write the data in a format that you can read. ‣ If omitted, data will be written in binary format. ‣ best for large amounds of data load filename x y • This is the complimentary command to save. • Reads variables x and y from file filename ‣ If variables are omitted, all variables are loaded... Saving variables to files & Loading variables from files clear; x = linspace(-pi,pi); y = cos(x); save myVariables x y; clear; load myVariables; who; plot(x,y); 2 Thursday, October 9, 2008
  • 3. Formatted Output in Matlab disp(x) - prints the contents of variable x. fprintf(...) - use for formatted printing • Allows much more control over output • Syntax: fprintf(‘text & formatting’,variables); • Text formatting: ‣ %a.bc ‣ a - minimum width of output buffer ‣ b - number of digits past decimal point ‣ c - formatting scheme ‣ f - floating point (typical format) 12.345 ‣ e - scientific notation - 1.2345e1 ‣ s - string format x = [1.1 2.2 3.3 4.4]; y = 2*x; fprintf('Hello. (%1.3f,%1.3f), (%1.1f,%1.0f)n',... x(1),y(1),x(3),y(3)); Hello. (1.100,2.200), (3.3,7) Control Code Description Example n Begin a new line fprintf('hello.n'); t Insert a “tab” fprintf('thello.n'); insert a backslash fprintf('hello.n'); '' Insert a single quote fprintf('''hello.''n'); %% Insert a % sign fprintf(‘%%%1.2fn’,95.6); 3 Thursday, October 9, 2008
  • 4. Formatted Output - Examples fprintf('%6s%8sn','index','value'); fprintf('--------------n'); n = 5; a = zeros(5,1); for( i=1:5 ) a(i) = 2*i+1; fprintf('%6.0f%8.1fn',i,a(i)); end Repeat temperature conversion example using fprintf rather than disp. 4 Thursday, October 9, 2008
  • 5. File Output in MATLAB Three steps: • Open the file ‣ fid = fopen(filename,’w’); ‣ ‘w’ tells matlab that we want to WRITE to the file. ‣ see “help fopen” for more information. • Write to the file ‣ fprintf(fid,format,variables); • Close the file ‣ fclose(fid); Example: Temperature conversion example - write results to a file called “tempTable.dat” 5 Thursday, October 9, 2008
  • 6. File Input in MATLAB Import wizard “File→Import Data” • Allows you to import data from delimited files (spreadsheets, etc) Importing “spreadsheet” data • dlmread - import data from a delimited file (you choose the delimiter) • xlsread - import data from Excel. General file input - three steps: • fid=fopen(filename,’r’) - open a file to allow detailed input control. ‣ ‘r’ tells matlab that we want to READ from the file. • a=fscanf(fid,format,size); ‣ Works like file writing, but use fscanf rather than fprintf. ‣ fid - file id that you want to read from ‣ format - how you want to save the information (string, number) ‣ ‘%s’ to read a string, ‘%f’ to read a floating point number, ‘%e’ to read scientific notation. ‣ size - how many entries to read. ‣ feof(fid) - returns true if end of file, false otherwise. • fclose(fid); 6 Thursday, October 9, 2008
  • 7. clear; clc; % open a file to read - first line contains % the order of the polynomial. Second line % contains the polynomial coefficients. fid = fopen('poly.dat',’r’); % read the order of the polynomial n = fscanf(fid,'%f',1); % read all of the polynomial coefficients a = fscanf(fid,'%f',n+1); 4 1.0 2 0.02 4.0 0 2 0 1 2.3 “poly.dat” “poly.dat” File Input - Example 1 p(x) = 4 ! i=0 ai xi = a0 + a1x + a2x2 + a3x3 + a4x4 p(x) = n ! i=0 ai xi General form of an nth order polynomial: For a quartic (n=4) we have: 7 Thursday, October 9, 2008
  • 8. File Input - Example 2 Have the user specify the number of elements in a molecule in a file. Read the file and then output the molecular weight. Include H C O N S. Steps: 1. Set up the MW vector: mw=[mwH mwC mwO mwN mwS]; 2. Set up the composition vector: nAtoms=zeros(1,5); 3. Open the file 4. While we aren’t at the end of the file 1. read a line. 2. Assign the proper entry in nAtoms 5. Calculate the mixture molecular weight & output it. Convention: input lines look like: Element Number Example: C 1 H 4 8 Thursday, October 9, 2008
  • 9. Excel - I/O Reading delimited data • File→Open (may need to select file type to be “all files”) • An import wizard opens, allowing you to select delimiters 9 Thursday, October 9, 2008