ENG1060 Lecture 4: Week 4 fprintf and sprintf

82 views2 pages
fprintf function
To#either#command#window#or#file
Prints#multiple#variables#in#one#formatted#line#of#text,
instead#of#disp() which#can#only#print#1#at#a#time
Can#store#both#numbers#and#strings.
fprintf('format',var1,var2,var3,…)
'format' >>>#string#containing#placeholders for#your#variables
Placeholders#denoted#w/#%symbol
%f##>>>#fixed#point#
%e >>>#exponential#
%d >>>#decimal,#no#decimal#if#integer#variable
%g >>>#whichever#of#%f#or#%e#is#shorter
%s >>>#string
Special#characters#for#better#formatting/display#of#results
\n >>>#new#line
\t >>>#tab
\b >>># backspace
' ' >>># single#quote
%% >>># %
var1,var2,var3 >>>#variables for#placeholders,#appear#in#order#
written
Can#specify:#%<width>.<precision><specifier>
Width =#number#of#spaces#used#for#printing
Precision =#number#of#decimal#places
Works#with#matrices/vectors#and#prints#each#element
Vector:#printing#starts#w/#first#to#last#elem
X = [3 4 5 6 7 8]
fprintf('X is comprised of %.3f\n',X)
Matrix:#printing#starts#w/#first#column,#then#moves
y = [5 6 7; 38 02 2; 83 29 10]
fprintf('y is comprised of %.3f\n',y)
sprintf function
Like#fprintf#but#instead,#resulting#
output#is#string
Stores strings.
Useful#for#eg.#plot#titles
fprintf('X is %3.1f%%
d',0.11111)
X is 0.1%d
Week$4:$fprintf$and$sprintf
Saturday,#12#August#2017
20:57
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Prints multiple variables in one formatted line of text, instead of disp() which can only print 1 at a time. "format" >>> string containing placeholders for your variables. Specifiers for notations, telling fprintf where and how variable values shld be printed. %d >>> decimal, no decimal if integer variable. %g >>> whichever of %f or %e is shorter. " " >>> single quote var1,var2,var3 >>> variables for placeholders, appear in order written. Width = number of spaces used for printing. Vector: printing starts w/ first to last elem. X = [3 4 5 6 7 8] fprintf("x is comprised of %. 3f\n",x) Matrix: printing starts w/ first column, then moves y = [5 6 7; 38 02 2; 83 29 10] fprintf("y is comprised of %. 3f\n",y) sprintf function. Like fprintf but instead, resulting output is string. Useful for eg. plot titles fprintf("x is %3. 1f%% d",0. 11111)

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents