Matlab - Tutor4 - Logic
Matlab - Tutor4 - Logic
Data is saved in the form of arrays. When we display the data, it comes in
the form of an array, and that may not be as representable as always. So,
there are some formatting operators available in the MATLAB to convert the
data to text and format the output as per our requirements.
Example:
Output:
>> t
t =
'Displaying 3 random numbers:
1)0.957167
2)0.49
3)0.800280'
o The conversion character is an only required field among all six fields.
o The conversion character must always lead by a % percent sign.
o And no space character is allowed in between the fields of formatting
operator.
Syntax Example:
PlayNext
Mute
Duration 18:10
Loaded: 2.94%
Â
Fullscreen
Conversion Character
The conversion character is an only required field, and it specifies the
notation of the output. The conversion character is denoted by a single
alphabetical character and appears last in the format specifier.
1. c Single character
5. f Fixed-point notation
6. g The more compact of the %e or %f (insignificant z
print)
Example:
1. >>p=100;
2. >>ch = 'cdeEfgGosuxX';
3.
4. >>res = sprintf("%c\t\t%c \n%c\t\t%d \n%c\t\t%e \n%c\t\t%E"+...
5. "\n%c\t\t%f \n%c\t\t%g \n%c\t\t%G \n%c\t\t%o"+...
6. "\n%c\t\t%s\n%c\t\t%u\n%c\t\t%x\n%c\t\t%X?, ...
7. ch(1),p,ch(2),p,+ch(3),p,ch(4),p,ch(5),p,ch(6),+...
8. p,ch(7),p,ch(8),p,ch(9),p,ch(10),p,ch(11),p,ch(12),p);
Output:
>> res
res =
"c d
d 100
e 1.000000e+02
E 1.000000E+02
f 100.000000
g 100
G 100
o 144
s d
u 100
x 64
X 64"
Subtype
The subtype field is represented by a single alphabetic character that comes
immediately before the conversion character. The conversion characters %o,
%u, %x, and %X treat data input as integers, without the subtype field. So,
use one of the following subtype specifiers to treat data input as floating-
point values and convert them to octal, decimal, or hexadecimal
representations.
Subtype
Description
specifier
Treats input data as double-precision floating-point values rather tha
b
integers.
Treats input data as single-precision floating-point values rather tha
t
integers.
Example:
1. >> p = 100;
2.
3. >> res = sprintf('before using subtype:\t%u\nafter using subtype:\t%tu',p,p);
4. </pre>
Output:
>> res
res =
Precision
The precision field is denoted by a non-negative integer that immediately
comes after a period (dot). It is used with %f, %e, and %E operators and
indicates the number of digits to display to the right of the decimal point.
Example:
Output:
>> res
res =
Example:
Output:
>> res
res =
'output without and with field width: |3.033000e+02| | 3.033000e+02| |
303.300000| | 303.300000|'
Flags
The flags field controls the additional formatting of the output. The
characters used for the flags describe mainly the spacing, padding, and text
alignment.
Flags Description
character
Plus sign (+) Right-justify the output if the output is a text, and add a leading plus
sign character if it is number
The pound The pound sign flag is a special case as it is somehow different from
sign (#) other flags. It modifies selected numeric conversions as:
o Prints 0, 0x, or 0X prefix for %o, %x, or %X operators.
o Prints decimal point even when precision is 0 for %f, %e, or %E
operators.
o It doesn't remove trailing zeroes or decimal points for %g or %G
operators.
1. >> sprintf('right-justify:|%+12s|\nleft-justify:|%-12.2f|',"flags",10);
Output:
>> ans
ans =
'right-justify:| flags|
left-justify:|10.00 |'
Output:
>> b
b =
1. >> a=sprintf('%#.0f',10.00);
ADVERTISEMENT
Output:
>> a
a =
'10.'
Identifiers
The output functions such as sprintf prints the output in the same sequence
as it takes input arguments. So, use the identifier to produce the output in a
custom specified order. Identifiers are the integer values and come
immediately after the % percent sign following by a $ dollar sign.
Default order
Example:
Output:
>> t1
t1 =
Example:
Output:
>> t2
t2 =
%%
Backslash \\
Alarm \a
Backspace \b
Form feed \f
New line \n
Carriage return \r
Horizontal tab \t
Vertical tab \v
Example:
Output:
>> t3
t3 =
Example:
Output:
>>t4 =
Syntax:
1. if expression
2. Statements
3. end
Output:
a = 12×2
227 183
134 177
28 196
207 72
85 174
74 140
187 100
3 16
13 196
167 85
total hours of job done by you in a year are: 3085
Great...you earned a bonus amount of Rs. 10000
Syntax:
1. if expression
2. Statements
3. else
4. Statements
5. end
Output:
a = 15
an odd number
Example2:
Using if-else statement to determine the largest of two number
Output:
PauseNext
Mute
Duration 18:10
Loaded: 2.94%
Â
Fullscreen
Enter number 1: 15
Enter number 2:20
num1 is less than num2
Syntax:
1. if expression
2. Statements
3. elseif expression
4. Statements
5. else
6. Statements
7. end
Example1:
Output:
n = 9
you are younger