SlideShare a Scribd company logo
1 of 14
CSCI 101
Formatted Output
fprintf function, read from/write to
file
Overview
• fprintf function
• fprintf a table
• Plotting 2D graph
• Saving to a file
• Reading from a file
fprintf function
• fprintf can be used to control how output is
displayed
fprintf(format,data1,data2, …)
• format is a string specifying how to display the
data
• datax are variables or values
• Example:
fprintf(‘x=%f and y=%f’,x,y)
fprintf function
• The %d characters in the format string are
called “conversion characters”
• Each conversion character is a data place-
holder in the format string
• Each conversion character has a number of
optional formatting parameters
• These parameters go between the % and the
conversion character (d, f, etc.)
fprintf conversion characters
%? Results
%d display value in standard decimal notation
%e display value in exponential form
%f display value in fixed(floating)-point form
%s display a string of characters (not used with numbers)
• For complete list of options, use >> help fprintf
Examples:
%d like 15
%f like 10.123400
%e like 1.123400e-03
fprintf Examples
x=3; y=4; z=50;
fprintf(‘This is a test ’);
fprintf(‘num1=%d num2=%f num3=%e’,x,y,z);
This is a test num1=3 num2=4.000000 num3=5.000000e+01
501,501.000000,5.010000e+02
z=501
fprintf(‘%d,%f,%e’,z,z,z);
fprintf Escape characters
• To display a “%”, use ‘%%’ in fprintf’s format
• To move to new line use n
• To display backslash use ‘’
• To display single quotation use two single
quotations
x=3;
fprintf(‘This is a test n’);
fprintf(‘ ‘’ num1=%d/100 = %f %%  ‘’ ’,x,x/100);
This is a test
‘ num1=3/100 = 0.030000%  ’
fprintf function
%-8.2f
Number insertion
marker
Optional Flag
indicating positioning
inside the “field”
Field width &
decimal precision
Conversion
character
fprintf %f Examples
x=3.1253;
fprintf(‘X= %10.3f, %.0f, %%n’,x,x);
fprintf(‘x= %-10.3f, %.0f, n %%n’,x,x);
fprintf(‘x= %-10.2f, %fn’,x,x);
X = 3 . 1 2 5 , 3 , %
x = 3 . 1 2 5 , 3 ,
%
x = 3 . 1 3 , 3 . 1 2 5 3 0 0
fprintf a table
fprintf(‘Angle Sinn’);
for d=-90:10:90
r=d*pi/180;
s=sin(r);
fprintf(‘%-10d %-10.2fn’,d,s);
end
Angle Sin
-90 -1.00
-80 -0.98
-70 -0.94
-60 -0.87
-50 -0.77
-40 -0.64
-30 -0.50
-20 -0.34
-10 -0.17
0 0.00
10 0.17
20 0.34
30 0.50
40 0.64
50 0.77
60 0.87
70 0.94
80 0.98
90 1.00
Plotting 2D graph
i=1;
fprintf('Angle Sinn');
for d=-360:1:360
r=d*pi/180;
s(i)=sin(r);
a(i)=d;
fprintf('%-10d%-10.2fn',d,s(i));
i=i+1;
end
plot(a,s);
Try it in MATLAB
Plotting 2D graph
(Adding title and labels)
i=1;
fprintf('Angle Sinn');
for d=-360:360
r=d*pi/180;
s(i)=sin(r);
a(i)=d;
fprintf('%-10d %-10.2fn',d,s(i));
i=i+1;
end
plot(a,s);
title(’Sine Wave’);
xlabel(’Angle’); ylabel(’Sin’);
grid;
Try it in MATLAB
Saving to a file
f=fopen('test.txt','w'); % Open/create a file named test.txt for writing
i=1;
fprintf(f,'Angle Sinn'); %write to the opened file
for d=-360:360
r=d*pi/180;
s(i)=sin(r);
a(i)=d;
fprintf(f,'%-10d%-10.2fn',d,s(i));
i=i+1;
end
plot(a,s);
fclose(f); % close the opened file
Try it in MATLAB and check the file test.txt
Reading from a file
fileID = fopen('test.txt','r');
header=fgetl(fileID); % get and ignore header
formatSpec = '%d %f';
sizeA = [2 Inf];
A = fscanf(fileID,formatSpec,sizeA);
plot(A(1,:),A(2,:));
fclose(fileID);
A =
-90.0000 -80.0000 -70.0000 -60.0000 -50.0000
-1.0000 -0.9800 -0.9400 -0.8700 -0.7700
A=A’
Angle Sin
-90 -1.00
-80 -0.98
-70 -0.94
-60 -0.87
-50 -0.77
-40 -0.64
-30 -0.50
-20 -0.34
-10 -0.17
0 0.00
10 0.17
20 0.34
30 0.50
40 0.64
50 0.77
60 0.87
70 0.94
80 0.98
90 1.00

More Related Content

What's hot

Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C BUBT
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in cBUBT
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Abou Bakr Ashraf
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
control stucture in c language
control stucture in c language control stucture in c language
control stucture in c language abdulahi45
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9Rumman Ansari
 
Lesson 4.3 First and Second Derivative Theory
Lesson 4.3 First and Second Derivative TheoryLesson 4.3 First and Second Derivative Theory
Lesson 4.3 First and Second Derivative TheorySharon Henry
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in cBUBT
 
Decision Control Structure If & Else
Decision Control Structure If & ElseDecision Control Structure If & Else
Decision Control Structure If & ElseAbdullah Bhojani
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CBUBT
 
Managing input and output operations in c
Managing input and output operations in cManaging input and output operations in c
Managing input and output operations in cniyamathShariff
 
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 6  Balagurusamy Programming ANSI  in cChapter 6  Balagurusamy Programming ANSI  in c
Chapter 6 Balagurusamy Programming ANSI in cBUBT
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programmingRabin BK
 
Gaussian_GaussJordan
Gaussian_GaussJordanGaussian_GaussJordan
Gaussian_GaussJordanJames Little
 
Increasing and decreasing functions ap calc sec 3.3
Increasing and decreasing functions ap calc sec 3.3Increasing and decreasing functions ap calc sec 3.3
Increasing and decreasing functions ap calc sec 3.3Ron Eick
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branchingSaranya saran
 

What's hot (19)

Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
 
If else
If elseIf else
If else
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
control stucture in c language
control stucture in c language control stucture in c language
control stucture in c language
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
Lesson 4.3 First and Second Derivative Theory
Lesson 4.3 First and Second Derivative TheoryLesson 4.3 First and Second Derivative Theory
Lesson 4.3 First and Second Derivative Theory
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in c
 
Decision Control Structure If & Else
Decision Control Structure If & ElseDecision Control Structure If & Else
Decision Control Structure If & Else
 
Basic Input and Output
Basic Input and OutputBasic Input and Output
Basic Input and Output
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
 
Managing input and output operations in c
Managing input and output operations in cManaging input and output operations in c
Managing input and output operations in c
 
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 6  Balagurusamy Programming ANSI  in cChapter 6  Balagurusamy Programming ANSI  in c
Chapter 6 Balagurusamy Programming ANSI in c
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
 
Gaussian_GaussJordan
Gaussian_GaussJordanGaussian_GaussJordan
Gaussian_GaussJordan
 
Increasing and decreasing functions ap calc sec 3.3
Increasing and decreasing functions ap calc sec 3.3Increasing and decreasing functions ap calc sec 3.3
Increasing and decreasing functions ap calc sec 3.3
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 

Similar to Csci101 lect05 formatted_output

T03 a basicioprintf
T03 a basicioprintfT03 a basicioprintf
T03 a basicioprintfteach4uin
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in CMuthuganesh S
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabMohan Raj
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Input output functions
Input output functionsInput output functions
Input output functionshyderali123
 
Mba Ebooks ! Edhole
Mba Ebooks ! EdholeMba Ebooks ! Edhole
Mba Ebooks ! EdholeEdhole.com
 
Mba Ebooks ! Edhole
Mba Ebooks ! EdholeMba Ebooks ! Edhole
Mba Ebooks ! EdholeEdhole.com
 
miniLesson on the printf() function
miniLesson on the printf() functionminiLesson on the printf() function
miniLesson on the printf() functionChristine Wolfe
 
MATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output CommandsMATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output CommandsShameer Ahmed Koya
 
Format string
Format stringFormat string
Format stringVu Review
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 FocJAYA
 

Similar to Csci101 lect05 formatted_output (20)

Introduction to Input/Output Functions in C
Introduction to Input/Output Functions in CIntroduction to Input/Output Functions in C
Introduction to Input/Output Functions in C
 
T03 a basicioprintf
T03 a basicioprintfT03 a basicioprintf
T03 a basicioprintf
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
Input And Output
 Input And Output Input And Output
Input And Output
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
CHAPTER 4
CHAPTER 4CHAPTER 4
CHAPTER 4
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
CPU INPUT OUTPUT
CPU INPUT OUTPUT CPU INPUT OUTPUT
CPU INPUT OUTPUT
 
Input output functions
Input output functionsInput output functions
Input output functions
 
Mba Ebooks ! Edhole
Mba Ebooks ! EdholeMba Ebooks ! Edhole
Mba Ebooks ! Edhole
 
Mba Ebooks ! Edhole
Mba Ebooks ! EdholeMba Ebooks ! Edhole
Mba Ebooks ! Edhole
 
miniLesson on the printf() function
miniLesson on the printf() functionminiLesson on the printf() function
miniLesson on the printf() function
 
dinoC_ppt.pptx
dinoC_ppt.pptxdinoC_ppt.pptx
dinoC_ppt.pptx
 
MATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output CommandsMATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output Commands
 
Format string
Format stringFormat string
Format string
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
Cbasic
CbasicCbasic
Cbasic
 

More from Elsayed Hemayed

14 cie552 camera_calibration
14 cie552 camera_calibration14 cie552 camera_calibration
14 cie552 camera_calibrationElsayed Hemayed
 
12 cie552 object_recognition
12 cie552 object_recognition12 cie552 object_recognition
12 cie552 object_recognitionElsayed Hemayed
 
11 cie552 image_featuresii_sift
11 cie552 image_featuresii_sift11 cie552 image_featuresii_sift
11 cie552 image_featuresii_siftElsayed Hemayed
 
10 cie552 image_featuresii_corner
10 cie552 image_featuresii_corner10 cie552 image_featuresii_corner
10 cie552 image_featuresii_cornerElsayed Hemayed
 
09 cie552 image_featuresi
09 cie552 image_featuresi09 cie552 image_featuresi
09 cie552 image_featuresiElsayed Hemayed
 
08 cie552 image_segmentation
08 cie552 image_segmentation08 cie552 image_segmentation
08 cie552 image_segmentationElsayed Hemayed
 
07 cie552 image_mosaicing
07 cie552 image_mosaicing07 cie552 image_mosaicing
07 cie552 image_mosaicingElsayed Hemayed
 
06 cie552 image_manipulation
06 cie552 image_manipulation06 cie552 image_manipulation
06 cie552 image_manipulationElsayed Hemayed
 
05 cie552 image_enhancement
05 cie552 image_enhancement05 cie552 image_enhancement
05 cie552 image_enhancementElsayed Hemayed
 
04 cie552 image_filtering_frequency
04 cie552 image_filtering_frequency04 cie552 image_filtering_frequency
04 cie552 image_filtering_frequencyElsayed Hemayed
 
03 cie552 image_filtering_spatial
03 cie552 image_filtering_spatial03 cie552 image_filtering_spatial
03 cie552 image_filtering_spatialElsayed Hemayed
 
02 cie552 image_andcamera
02 cie552 image_andcamera02 cie552 image_andcamera
02 cie552 image_andcameraElsayed Hemayed
 
Csci101 lect04 advanced_selection
Csci101 lect04 advanced_selectionCsci101 lect04 advanced_selection
Csci101 lect04 advanced_selectionElsayed Hemayed
 
Csci101 lect10 algorithms_iii
Csci101 lect10 algorithms_iiiCsci101 lect10 algorithms_iii
Csci101 lect10 algorithms_iiiElsayed Hemayed
 
Csci101 lect09 vectorized_code
Csci101 lect09 vectorized_codeCsci101 lect09 vectorized_code
Csci101 lect09 vectorized_codeElsayed Hemayed
 
Csci101 lect08b matlab_programs
Csci101 lect08b matlab_programsCsci101 lect08b matlab_programs
Csci101 lect08b matlab_programsElsayed Hemayed
 
Csci101 lect08a matlab_programs
Csci101 lect08a matlab_programsCsci101 lect08a matlab_programs
Csci101 lect08a matlab_programsElsayed Hemayed
 
Csci101 lect07 algorithms_ii
Csci101 lect07 algorithms_iiCsci101 lect07 algorithms_ii
Csci101 lect07 algorithms_iiElsayed Hemayed
 
Csci101 lect06 advanced_looping
Csci101 lect06 advanced_loopingCsci101 lect06 advanced_looping
Csci101 lect06 advanced_loopingElsayed Hemayed
 

More from Elsayed Hemayed (20)

14 cie552 camera_calibration
14 cie552 camera_calibration14 cie552 camera_calibration
14 cie552 camera_calibration
 
12 cie552 object_recognition
12 cie552 object_recognition12 cie552 object_recognition
12 cie552 object_recognition
 
11 cie552 image_featuresii_sift
11 cie552 image_featuresii_sift11 cie552 image_featuresii_sift
11 cie552 image_featuresii_sift
 
10 cie552 image_featuresii_corner
10 cie552 image_featuresii_corner10 cie552 image_featuresii_corner
10 cie552 image_featuresii_corner
 
09 cie552 image_featuresi
09 cie552 image_featuresi09 cie552 image_featuresi
09 cie552 image_featuresi
 
08 cie552 image_segmentation
08 cie552 image_segmentation08 cie552 image_segmentation
08 cie552 image_segmentation
 
07 cie552 image_mosaicing
07 cie552 image_mosaicing07 cie552 image_mosaicing
07 cie552 image_mosaicing
 
06 cie552 image_manipulation
06 cie552 image_manipulation06 cie552 image_manipulation
06 cie552 image_manipulation
 
05 cie552 image_enhancement
05 cie552 image_enhancement05 cie552 image_enhancement
05 cie552 image_enhancement
 
04 cie552 image_filtering_frequency
04 cie552 image_filtering_frequency04 cie552 image_filtering_frequency
04 cie552 image_filtering_frequency
 
03 cie552 image_filtering_spatial
03 cie552 image_filtering_spatial03 cie552 image_filtering_spatial
03 cie552 image_filtering_spatial
 
02 cie552 image_andcamera
02 cie552 image_andcamera02 cie552 image_andcamera
02 cie552 image_andcamera
 
01 cie552 introduction
01 cie552 introduction01 cie552 introduction
01 cie552 introduction
 
Csci101 lect04 advanced_selection
Csci101 lect04 advanced_selectionCsci101 lect04 advanced_selection
Csci101 lect04 advanced_selection
 
Csci101 lect10 algorithms_iii
Csci101 lect10 algorithms_iiiCsci101 lect10 algorithms_iii
Csci101 lect10 algorithms_iii
 
Csci101 lect09 vectorized_code
Csci101 lect09 vectorized_codeCsci101 lect09 vectorized_code
Csci101 lect09 vectorized_code
 
Csci101 lect08b matlab_programs
Csci101 lect08b matlab_programsCsci101 lect08b matlab_programs
Csci101 lect08b matlab_programs
 
Csci101 lect08a matlab_programs
Csci101 lect08a matlab_programsCsci101 lect08a matlab_programs
Csci101 lect08a matlab_programs
 
Csci101 lect07 algorithms_ii
Csci101 lect07 algorithms_iiCsci101 lect07 algorithms_ii
Csci101 lect07 algorithms_ii
 
Csci101 lect06 advanced_looping
Csci101 lect06 advanced_loopingCsci101 lect06 advanced_looping
Csci101 lect06 advanced_looping
 

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 

Recently uploaded (20)

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 

Csci101 lect05 formatted_output

  • 1. CSCI 101 Formatted Output fprintf function, read from/write to file
  • 2. Overview • fprintf function • fprintf a table • Plotting 2D graph • Saving to a file • Reading from a file
  • 3. fprintf function • fprintf can be used to control how output is displayed fprintf(format,data1,data2, …) • format is a string specifying how to display the data • datax are variables or values • Example: fprintf(‘x=%f and y=%f’,x,y)
  • 4. fprintf function • The %d characters in the format string are called “conversion characters” • Each conversion character is a data place- holder in the format string • Each conversion character has a number of optional formatting parameters • These parameters go between the % and the conversion character (d, f, etc.)
  • 5. fprintf conversion characters %? Results %d display value in standard decimal notation %e display value in exponential form %f display value in fixed(floating)-point form %s display a string of characters (not used with numbers) • For complete list of options, use >> help fprintf Examples: %d like 15 %f like 10.123400 %e like 1.123400e-03
  • 6. fprintf Examples x=3; y=4; z=50; fprintf(‘This is a test ’); fprintf(‘num1=%d num2=%f num3=%e’,x,y,z); This is a test num1=3 num2=4.000000 num3=5.000000e+01 501,501.000000,5.010000e+02 z=501 fprintf(‘%d,%f,%e’,z,z,z);
  • 7. fprintf Escape characters • To display a “%”, use ‘%%’ in fprintf’s format • To move to new line use n • To display backslash use ‘’ • To display single quotation use two single quotations x=3; fprintf(‘This is a test n’); fprintf(‘ ‘’ num1=%d/100 = %f %% ‘’ ’,x,x/100); This is a test ‘ num1=3/100 = 0.030000% ’
  • 8. fprintf function %-8.2f Number insertion marker Optional Flag indicating positioning inside the “field” Field width & decimal precision Conversion character
  • 9. fprintf %f Examples x=3.1253; fprintf(‘X= %10.3f, %.0f, %%n’,x,x); fprintf(‘x= %-10.3f, %.0f, n %%n’,x,x); fprintf(‘x= %-10.2f, %fn’,x,x); X = 3 . 1 2 5 , 3 , % x = 3 . 1 2 5 , 3 , % x = 3 . 1 3 , 3 . 1 2 5 3 0 0
  • 10. fprintf a table fprintf(‘Angle Sinn’); for d=-90:10:90 r=d*pi/180; s=sin(r); fprintf(‘%-10d %-10.2fn’,d,s); end Angle Sin -90 -1.00 -80 -0.98 -70 -0.94 -60 -0.87 -50 -0.77 -40 -0.64 -30 -0.50 -20 -0.34 -10 -0.17 0 0.00 10 0.17 20 0.34 30 0.50 40 0.64 50 0.77 60 0.87 70 0.94 80 0.98 90 1.00
  • 11. Plotting 2D graph i=1; fprintf('Angle Sinn'); for d=-360:1:360 r=d*pi/180; s(i)=sin(r); a(i)=d; fprintf('%-10d%-10.2fn',d,s(i)); i=i+1; end plot(a,s); Try it in MATLAB
  • 12. Plotting 2D graph (Adding title and labels) i=1; fprintf('Angle Sinn'); for d=-360:360 r=d*pi/180; s(i)=sin(r); a(i)=d; fprintf('%-10d %-10.2fn',d,s(i)); i=i+1; end plot(a,s); title(’Sine Wave’); xlabel(’Angle’); ylabel(’Sin’); grid; Try it in MATLAB
  • 13. Saving to a file f=fopen('test.txt','w'); % Open/create a file named test.txt for writing i=1; fprintf(f,'Angle Sinn'); %write to the opened file for d=-360:360 r=d*pi/180; s(i)=sin(r); a(i)=d; fprintf(f,'%-10d%-10.2fn',d,s(i)); i=i+1; end plot(a,s); fclose(f); % close the opened file Try it in MATLAB and check the file test.txt
  • 14. Reading from a file fileID = fopen('test.txt','r'); header=fgetl(fileID); % get and ignore header formatSpec = '%d %f'; sizeA = [2 Inf]; A = fscanf(fileID,formatSpec,sizeA); plot(A(1,:),A(2,:)); fclose(fileID); A = -90.0000 -80.0000 -70.0000 -60.0000 -50.0000 -1.0000 -0.9800 -0.9400 -0.8700 -0.7700 A=A’ Angle Sin -90 -1.00 -80 -0.98 -70 -0.94 -60 -0.87 -50 -0.77 -40 -0.64 -30 -0.50 -20 -0.34 -10 -0.17 0 0.00 10 0.17 20 0.34 30 0.50 40 0.64 50 0.77 60 0.87 70 0.94 80 0.98 90 1.00