disp( )
'disp' is
the basic command used to display value of a variable or a string. It can
display value of only one variable or a string at a time. It can’t display
multiple values in single command.
Syntax
disp(A)
disp(A)
displays the value of variable A without printing the variable name. The
argument can be a string, a numeric or an expression. If the variable contains
an empty array, returns without displaying anything.
Examples
>>
A = [1 2 3 4 5];
>>
disp (A)
1
2 3 4
5
>>
disp([1 2 3 4 5])
1
2 3 4
5
>>
S = 'Electrical';
>>
disp(S)
Electrical
>> disp('Electrical')
Electrical
>>
disp(A.*2)
2
4 6 8
10
If the
data is mixed with numeric and text, you have to use multiple disp commands.
>>
disp('Today is April'), disp(1)
Today
is April
1
Here
the text and the number are displayed in different lines. They can be printed
in the same line with following code
>>
m = ['Today is April ', num2str(1)];
>>
disp(m)
Today
is April 1
Tips:
Display Hyperlink in Command Window
Display
a link to a Web page by including HTML hyperlink code as input to disp. For
example, display a link to my blog site.
>>
X = '<a href = "http://electricalenggtutorial.blogspot.com">
Electrical Engineering Tutorial</a>';
>>
disp(X)
Electrical Engineering Tutorial
fprintf( )
'fprintf' is used to formats data and displays the results on the screen and to write the
data in to a text file. It can mix numbers and text in output and have full
control of output display formats.
Syntax
fprintf(‘text’)
will display the text in the command window without any formatting. This is the
simple form of the function.
fprintf(format,
A, ...) formats data stored in the variable A and displays the results on the screen.
fprintf(FID,
format, A, ...) applies the format to all elements of array A and any
additional array arguments in column order, and writes the data to a text
file. FID is an integer file identifier.
Examples
>>fprintf('Matlab Programing Tips')
Matlab
Programing Tips>>
Note
that the next prompt is coming in the same line. This can be solved by
inserting \n after the text.
>>
fprintf('Matlab Programing Tips\n')
Matlab
Programing Tips
>>
\n can
be used to display the text in multiple lines. This is done by inserting \n
before the character that will start the new line. For example, insert \n after
each word in the previous example.
>>
fprintf('Matlab\n Programing\n Tips\n')
Matlab
Programing
Tips
When a
program has more than one fprintf command, the display generated is continuous
(the fprintf command does not automatically start a new line). The following
lines are from a script file.
>> fprintf('Matlab Programing Tips')
>> fprintf('are very useful\n')
the
program will give an output in the command window as:
Matlab
Programing Tipsare very useful
Formatting
Operator
To
display data in a variable, the formatting operators are used. A
formatting operator starts with a percentage sign,
%
, and ends with a conversion
character. The table shows conversion characters to format numeric and character
data as text.
Value
Type
|
Conversion
|
Details
|
Integer,
signed
|
%d or %i
|
Base
10
|
Integer,
unsigned
|
%u
|
Base
10
|
%o
|
Base
8 (octal)
|
|
%x
|
Base
16 (hexadecimal), lowercase letters a–f
|
|
Floating-point
number
|
%f
|
Fixed-point
notation (Use a precision operator to specify the number of digits after the
decimal point.)
|
%e
|
Exponential
notation, such as 3.141593e+00 (Use a precision operator to specify
the number of digits after the decimal point.)
|
|
%g
|
The
more compact of %e or %f, with no trailing zeros (Use a
precision operator to specify the number of significant digits.)
|
|
Characters
|
%c
|
Single
character
|
%s
|
Character
vector
|
Examples
>>
age = 35;
>>
weight = 75.25;
>>
fprintf( 'Joe is %d weighs %f kilos\n', age, weight )
Joe is
35 weighs 75.250000 kilos
%d
defines age as an integer and %f defines weight as floating point number.
The
optional identifier, flags, field width, precision, and subtype operators
further define the format of the output text.
·
Identifier
– defines the order for processing the function input arguments.
Use the syntax
n
$
, where n
represents
the positions of the other input arguments in the function call.
·
Flag
- can be one of the following three characters:
– Left-justifies the number within the
field.
+
Prints a sign character (+ or –)
in front of the number.
0 Adds zeros if the number is shorter
than the field.
·
Field
Width - Minimum number of characters to print. The function pads to
field width with spaces before the value.
·
Precision
- specifies the number of digits to be displayed to the right of
the decimal point.
Example:
>>
fprintf( 'Joe weighs %4.4f kilos\n', weight )
Joe
weighs 75.2500 kilos
>>
fprintf( 'Joe weighs %4.2f kilos\n', weight )
Joe
weighs 75.25 kilos
%4.4f
in the format specifies that the value as a floating-point number with a field
width of four digits, including four digits after the decimal point. %4.2f in
the format specifies that the value as a floating-point number with a field
width of four digits, including two digits after the decimal point.
Writing
data to a File
Write a
short table of the exponential function to a text file called
exp.txt
.
x = 0:.1:1;A = [x; exp(x)];fileID = fopen('exp.txt','w');
fprintf(fileID,'%6s %12s\n','x','exp(x)');
fprintf(fileID,'%6.2f %12.8f\n',A);
fclose(fileID);
The script open the file exp.txt and write the values of the x and exp(x) to the file.
fprintf(fileID,'%6s %12s\n','x','exp(x)') is used to set the header for each column. %6 define the width a 6 for values of x and %12 define the width as 12 for exp(x). fclose command is used to close the file.
Some special characters are used as format operators. So we can’t use those characters as ordinary text. This table shows how to represent special characters.
fclose(fileID);
The script open the file exp.txt and write the values of the x and exp(x) to the file.
fprintf(fileID,'%6s %12s\n','x','exp(x)') is used to set the header for each column. %6 define the width a 6 for values of x and %12 define the width as 12 for exp(x). fclose command is used to close the file.
Some special characters are used as format operators. So we can’t use those characters as ordinary text. This table shows how to represent special characters.
Special Character | Representation |
Single quotation mark | '' |
Percent character | %% |
Backslash | \\ |
Alarm | \a |
Backspace | \b |
Form feed | \f |
New line | \n |
Carriage return | \r |
Horizontal tab | \t |
Vertical tab | \v |
Character whose ASCII code is the hexadecimal number, N | \xN |
Character whose ASCII code is the octal number, N | \N |
sprintf( )
'sprintf' is the same as fprintf except that it returns the data in a Matlab string rather than writing to a file.
Syntax
str = fprintf(format, A, ...) formats data stored in the variable A and return to the variable str.
Example
>> w = sprintf( 'Joe weighs %2.4f kilos\n', weight )
w =
Joe weighs 75.2500 kilos
>> w = sprintf( 'Joe weighs %2.4f kilos\n', weight )
w =
Joe weighs 75.2500 kilos
Msgbox( )
msgbox(‘Message’) creates a message box with a OK button that contains the Message to be displayed. The size of the figure is automatically fit to wrap the message. The argument ‘Message’ may be a string vector, string matrix or cell array.
age = 35;
weight = 75.25;
msgbox( sprintf( 'Joe Age is %d years \n Weight is %2.2f kilos', age, weight ))
age = 35;
weight = 75.25;
msgbox( sprintf( 'Joe Age is %d years \n Weight is %2.2f kilos', age, weight ))
The message box title
and type can be set by the additional optional parameters.
msgbox(‘Message’,’Title’,’Icon’)
specifies the Title of the message box and Icon to display in the message box. Icon is 'none', 'error', 'help', 'warn', or 'custom'. The default is 'none'. This can be
used to create the error or warning messages in the programs.
>>
msgbox('The ID you entered in Invalid','Invalid ID','error');
Tips: The error and warning messages can be made by dedicated commands 'errordlg( ) and warndlg( )
0 comments:
Post a Comment