1. Review fprintf : plain characters and special characters directly into the output >> fprintf('abc') abc>> but >> fprintf('abc\n') % \n : new line (move to next line abc >> >> fprintf('abc\nabc\nabc\tabc\n') % \t : tab move right to next tab position abc % (usually at multiples of 6) abc abc abc >> fprintf('abc\nabc\t\123\n') % abc abc S % \123 is in octal 1*64+2*8+3=83 and 83 is ASCII/UNICODE for S fprintf string characters A to Z, a to z, 0 to 9 : printed as are \n : newline \t : tab \a : bell (in some keyboards) \b : backspace '' : ' \\ : \ %% : % \40 : ASCII/UNICODE with octal value 40 dec 32 is space \x20 : with hex value 20 dec 32 is space 2. fprintf: print variables names by using placeholders for Place holders most used %s : string %c : character (one character only) %d : base-10 integer %i : base-10 integer %x %X : hexadecimal with a-f or A-F %o : octal %f : real number If a number between % and letter it indicates REQUEST for length. If REQUEST is too narrow, it will extended as fit. If a number between % and letter it indicates REQUEST for length. If REQUEST is too narrow, it will extended as fit. n below is a integer number indicating the width (in chars) of a textbox into which the variable's value will be printed. . is the period of a real number as in n.m m is an integer number indicating number of decimal digits to the right of . m should be smaller than n When computing number of chars for a number we add negative sign integer digits decimal point . decimal digits. The n is a request. If the value can be printed within the textbox the request will be honored; if not it will be ignored. string values: %s Variations %ns %-ns %0ns integer values (base-10) %d Variations %nd %-nd %0nd %+nd integer hex values %x or %X %nx %-nx %0nx %+nx and the X variants real values %f %n.mf %-n.mf %0n.mf %+0n.mf Some -+0 might be combined >> fprintf('%sHi%sHo%s\n','abc','abc'abc'); abcHiabcHoabc >> fprintf('%-3sHi%-3sHo%-3s\n','a','b'c'); %-3s means string 3 char wide left justified a Hib Hoc 3. fprintf : strings Sting of characters and placeholders , arguments separated by commas only argument here is a string ---- ---- >> fprintf('%s','abc'); % %s is a placeholde : print there a string abc>> % Which string, the leftmost %s with the leftmost argument after the first % string >> fprintf('%s\n','abc'); abc >> >> fprintf('%sHi%sHo%s\n','abc','abc'abc'); abcHiabcHoabc >> fprintf('%sHi%sHo%s\n','a','b'c'); aHibHoc >> >> fprintf('%3sHi%3sHo%3s\n','a','b'c'); %3s means string 3 char wide right justified aHi bHo c >> >> fprintf('%-3sHi%-3sHo%-3s\n','a','b'c'); %-3s means string 3 char wide left justified a Hib Hoc >>fprintf('abcdefg') abcdefg>>fprintf('abcdefg\\''%%abcd\nefg') abcdefg\'%abcd efg>>fprintf('abcd abcd\40abcd\x20 abcd\n') abcd abcd abcd abcd >>fprintf('abcd abcd\40abcd\x20''abcd\n') abcd abcd abcd 'abcd >>fprintf('abcd abcd\40abcd\x20');fprintf('abcd\n'); abcd abcd abcd abcd s1='a'; s2='bc' s2 = bc s3='def'; s4='ghij' s4 = ghij s5='klmno'; >>fprintf('%s %s %s %s\n',s1,s2,s1,s3) a bc a def 4. fprintf : integers %d : right justified %nd : n an integer a decimal of width n %-d : left justified %+d : include sign even if not needed (eg positive) %0d : pad with leading zeros %+0d: pad and sign >>a=10; >>fprintf('%d\n',a); 10 >>fprintf('%5d\n',a); % Note a is a variable whose value is 10 ! 10 >>fprintf('%-5d\n',a); % Note a is a variable whose value is 10 ! 10 >>fprintf('%+5d\n',a); % Note a is a variable whose value is 10 ! +10 >>fprintf('%-+5d\n',a); % Note a is a variable whose value is 10 ! +10 >>a=10; >>fprintf('%d%x%X%o\n',a,a,a,a); 10aA12 >>fprintf('%d\n%x\n%X\n%o\n',a,a,a,a); % Note a is a variable whose value is 10 ! 10 a A 12 >> fprintf('%d\n',10); % right justified (default) but as many digits wide as needed 10 >> fprintf('%5d\n',10); % length five the 1 appears in 4th position and 0 in 5th 10 >> fprintf('%-5d\n',10); % left justified 10 >> fprintf('%+5d\n',10); % + is signed right justfied +10 >> fprintf('%05d\n',10); % padded zeros 00010 >> fprintf('%+05d\n',10); % padded zeros and sign +0010 >> fprintf('%+5d\n',-10); % + is signed right justfied -10 >>fprintf('%x %X %o %d\n',10,10,10,10); a A 12 10 >>fprintf('%x %X %o %d',10,10,10,10); a A 12 10>>fprintf('%x\n %X\n %o\n %d\n',10,10,10,10); a A 12 10 >>fprintf(' %x\n %X\n %o\n %d\n',10,10,10,10); a A 12 10 >>fprintf('%5d\n',a); 10 >>fprintf('%5d\n',4555555555); 4555555555 >>fprintf('%5d\n',123456789); 123456789 >>fprintf('%+5d\n',a); +10 >>fprintf('%+5d\n',b); -10 >>fprintf('%-5d\n',a); 10 >>fprintf('%-5d\n',b); -10 >>fprintf('%-+5d\n',b); -10 >>fprintf('%+-5d\n',b); -10 >>fprintf('%+5d\n',b); -10 >>fprintf('%-+5d\n',a); +10 >>fprintf('%-5d\n',b); -10 >>fprintf('%+-5d\n',a); +10 >>fprintf('%5d\n%5d',a,b); 10 -10>>fprintf('%+5d\n%+5d',a,b); +10 -10 5. fprintf : real .... trouble >> fprintf('10.3f',12345.6789); % request for a 10 wide real number with exactly 3 decimal digits 12345.679 % space , 5 integer digits decimal point and 3 total = 1+5+1+3= 10 >> fprintf('10.3f',12345.67); % number rounded to three decimal digits 12345.670 NOTE: 10 is a REQUEST. If sign + integer digits + decimal sign + requested (3) decimal digits exceeds 10 the exceeded width will be used a=1234.5678 a = 1.2346e+03 a a = >>format long a a = 1.234567800000000e+03 >>fprintf('%f\n',a) 1234.567800 >>fprintf('%11f\n',a) 1234.567800 >>fprintf('%14f\n',a) 1234.567800 >>fprintf('%10.3f\n',a) 1234.568 >>fprintf('%10.3d\n',a) 1.235e+03 >>fprintf('%1.3f\n',a) 1234.568 >>fprintf('%12.7f\n',a) 1234.5678000 >>fprintf('%12.7f\n',12345.5678) 12345.5678000 >>fprintf('%12.7f\n',a) 1234.5678000 >>fprintf('%-15.7f\n',a) 1234.5678000 >>fprintf('%-15.3f\n',a) 1234.568 >>fprintf('%15.3f\n',a) 1234.568 >>fprintf('%+015.3f\n',a) >>fprintf('%+015.3f\n',a) +0000001234.568 >>fprintf('%-+015.3f\n',a) +1234.568 >>fprintf('%+015.3f\n',-a) -0000001234.568 >>fprintf('%+4.3f\n',-a) -1234.568 6. Overview % { +,-,0,*,none} { digits digits.digits } { h,l,b,t } {d i x X o c s f e E g G } % : is the place holder +,-,0 : signed, left-justified, padded (if justification not specified, it means right) none : default right justification * : the number of arguments after the placeholder string is variable. Number of arguments determines the multiplicity of the placeholder digits digits.digits : 5 or 5.6 as in %5d or %4.2f is REQUEST for length h l : [for int values] short (half) and long types . If an integer default is 32-bit h means 16 and and l means 64 t b : [for real values] single precision t (32bit) and double precision b (64bit) d i : decimal integer (base-10 o : octal x X : hexadecimal c : single character s : string f : floating point integer E e : see format long/short e G g : see format long/short g