0% found this document useful (0 votes)
87 views24 pages

Plotting and User Defined Function

This document discusses various ways to plot and format data in MATLAB, including: 1) Drawing basic and refined plots, setting line styles, colors, and thickness. 2) Plotting multiple curves, using different line styles and markers. 3) Creating plots with multiple panels. 4) Defining external user functions to plot customized functions. 5) Formatting output text using conversion characters and operators. 6) Writing output to files and reading data from existing files.

Uploaded by

ashwinikunayak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
87 views24 pages

Plotting and User Defined Function

This document discusses various ways to plot and format data in MATLAB, including: 1) Drawing basic and refined plots, setting line styles, colors, and thickness. 2) Plotting multiple curves, using different line styles and markers. 3) Creating plots with multiple panels. 4) Defining external user functions to plot customized functions. 5) Formatting output text using conversion characters and operators. 6) Writing output to files and reading data from existing files.

Uploaded by

ashwinikunayak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 24

Plotting & User Defined Function

Drawing a curve
a = [0:0.5:5];
b = 2*a.^2 + 3*a -5;
plot(a,b)
Refine the plot: Line pattern, color, and
thickness
a = [0:0.5:5];
b = 2*a.^2 + 3*a -5;
plot(a,b,'-
or','MarkerFaceColor','g','LineWidth',2)
xlabel('X'); ylabel('Y');
legend('Test','Location','NorthWest')
Line Style

Line Style Description Resulting Line


'-' Solid line
'--' Dashed line
':' Dotted line
'-.' Dash-dotted line
'none' No line No line
Color Code
Color Name Short Name
'red' 'r'
'green' 'g'
'blue' 'b'
'cyan' 'c'
'magenta' 'm'
'yellow' 'y'
'black' 'k'
'white' 'w'
'none' Not applicable
Draw multiple curves
a = [0:0.5:5];
b = 2*a.^2 + 3*a -5;
c = 1.2*a.^2+4*a-3;
hold on
plot(a,b,'-or','MarkerFaceColor','g','LineWidth',2)
plot(a,c,'--ok','MarkerFaceColor','c','LineWidth',2)
xlabel('X'); ylabel('Y'); legend('Curve 1','Curve
2','Location','NorthWest')
Draw symbols
a = [0:0.5:5];
b = 2*a.^2 + 3*a -5;
c = 1.2*a.^2+4*a-3;
hold on
plot(a,b,'or','MarkerFaceColor','g','LineWidth',2)
plot(a,c,'ok','MarkerFaceColor','c','LineWidth',2)
xlabel('X'); ylabel('Y'); legend('Curve 1','Curve
2','Location','NorthWest')
Markers
Value Description
'o' Circle
'+' Plus sign
'*' Asterisk
'.' Point
'x' Cross
'square' or 's' Square
'diamond' or 'd' Diamond
'^' Upward-pointing triangle
'v' Downward-pointing triangle
'>' Right-pointing triangle
'<' Left-pointing triangle
'pentagram' or 'p' Five-pointed star (pentagram)
'hexagram' or 'h' Six-pointed star (hexagram)
'none' No markers
Plot with multiple panels
a = [0:0.5:5];
b = 2*a.^2 + 3*a -5;
c = 1.2*a.^2+4*a-3;
subplot(1,2,1)
plot(a,b,'-or','MarkerFaceColor','g','LineWidth',2)
xlabel('X'); ylabel('Y');
legend('Curve','Location','NorthWest')
subplot(1,2,2)
plot(a,c,'--ok','MarkerFaceColor','c','LineWidth',2)
xlabel('X'); ylabel('Y');
legend('Curve2','Location','NorthWest')
External (user-defined) function
myfunc1.m
function outcome = myfunc1(x)
outcome = 2*x^2 + 3*x + 7;

mainprog1.m
for n = 1:5
x = n*0.1;
z = myfunc1(x);
fprintf('x = %4.2f f(x) = %8.4f \r', x, z)
end
• Output:
x = 0.10 f(x) = 7.3200
x = 0.20 f(x) = 7.6800
x = 0.30 f(x) = 8.0800
x = 0.40 f(x) = 8.5200
x = 0.50 f(x) = 9.0000
Formatting Operator

A formatting operator starts with a percent sign, %, and


ends with a conversion character. The conversion
character is required. Optionally, you can specify
identifier, flags, field width, precision, and subtype
operators between % and the conversion character.
(Spaces are invalid between operators and are shown
here only for readability).
Conversion Character
Value Type Conversion Details
Integer, signed %d or %i Base 10
Integer, %u Base 10
unsigned %o Base 8 (octal)
%x Base 16 (hexadecimal), lowercase letters a–f
%X Same as %x, uppercase letters A–F
Floating-point %f Fixed-point notation (Use a precision operator to specify the number
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.)
%E Same as %e, but uppercase, 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.)
%G The more compact of %E or %f, with no trailing zeros (Use a
precision operator to specify the number of significant digits.)
Characters or %c Single character
strings %s Character vector or string array. The type of the output text is the
same as the type of formatSpec.
Identifier
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.
Example: ('%3$s %2$s %1$s %2$s','A','B','C') prints
input arguments 'A', 'B', 'C' as follows: C B A B.
Note: If an input argument is an array, you cannot
use identifiers to specify particular array elements
from that input argument.
A function with multiple input
parameters
myfunc2.m
function outcome = myfunc2(x,a,b,c)
outcome = a*x^2 + b*x + c;
mainprog2.m
for n = 1:5
x = n*0.1;
z = myfunc2(x,2,3,7);
fprintf('x = %4.2f f(x) = %8.4f \r',x,z)
end
• Output:
x = 0.10 f(x) = 7.3200
x = 0.20 f(x) = 7.6800
x = 0.30 f(x) = 8.0800
x = 0.40 f(x) = 8.5200
x = 0.50 f(x) = 9.0000
Open a file and write the output to the
file
fID1 = fopen('myoutput1.txt','w');
for n = 1:4
b1 = n ; b2 = n^2 ; b3 = n^3;
fprintf(fID1,'%7u %7u %7u \r\n,b1,b2,b3);
end
This program will produce no output on the
screen but will create a file called
"myoutput.txt" (under the same directory where
the Matlab program itself is stored). The content
of the file is
1 1 1
2 4 8
3 9 27
4 16 64
Read data from an existing file
fID1 = fopen('myoutput1.txt','r');
for n = 1:4
b = fscanf(fID1,'%7u %7u %7u \r',3);
btotal = b(1)+b(2)+b(3);
fprintf('%7u + %7u + %7u = %7u \r', b(1), b(2),
b(3), btotal)
end
Output:
1+ 1+ 1= 3
2+ 4+ 8= 14
3 + 9 + 27 = 39
4 + 16 + 64 = 84

You might also like