Loading
Hosting Unlimited Indonesia

Graphics


2.1. Using get and set

2.1.1. What is a handle?

A handle is a number assigned by MATLAB to a graphics object. For example, you can have a handle to each object in a plot that contains many objects such as lines, patches, and surfaces. Once you know the handle to an object, you can alter its properties. To find out what properties are associated with each object, see axis , figure , line , patch , surf , root , and image . If you wanted to get the handle to a line when you plot it, you would do the following:
handle=plot(x,y)

2.1.2. How do I use get and set?

You can either get or set a property of an object in the following manner:
get(handle,'PropertyName')
set(handle,'PropertyName','PropertyValue')
Two common handles are gca and gcf which stand for 'get current axes' and 'get current figure', respectively. If you use gca or gcf as the handle in your get or set statement, you will be able to change the properties of the current figure or current axes without previously defining their handles.
For more information, look at the MATLAB technical note written on this topic. It can be found on the ftp site in pub/tech-support/tech-notes/gr12.txt.

2.1.3. What properties can I control?

To find out about an object's properties, type the following:
set(handle)
This returns a list of all the properties associated with that object as well as each property's optional settings. The settings in { } are the default settings.

2.1.4. How do I change the default settings for an object's properties?

To set the default setting for an object, you first need to know the ancestor of the object whose property you wish to set. To find the parent of an object, type the following:
h=get(object's_handle,'Parent')
To set the default, type the following:
set(h,'DefaultObjectPropertyName','PropertyValue')
Don't put any spaces in the DefaultObjectPropertyName expression. A good example of how to do this is the following:
set(gca,'DefaultLineLineWidth',10)
Any line you plot after this statement will have a line width of 10.

2.1.5. How do I change the default settings back to their original settings?

Set the default settings just as explained above, but use factory for the property value. For example, if you changed the default line width to 10 as above and then you wanted to set it back to the original setting, you would type:
set(gca,'DefaultLineLineWidth','factory')

2.2. Properties of the Figure and the Axes

2.2.1. Fonts

How do I change the font of labels?
When you change the default font name and font size, the factory settings will still be used when drawing the title and lables on the screen. Following is a modified xlabel , ylabel , zlabel , and title to recognize changes to the default FontName and FontSize ; however, it would be just as easy to have it recognize changes to all the default text font properties. Below are the revised programs:
<------- XLABEL.M ------->
function xlabel(string)
% XLABEL X-axis labels for 2-D and 3-D plots.
% XLABEL('text') adds text below the X-axis on the current % axis.
%
% See also YLABEL, ZLABEL, TITLE, TEXT.
% Copyright (c) 1984-92 by The MathWorks, Inc.
h = get(gca,'xlabel');
ht = get(text,'fontname');
hs = get(text,'fontsize');
if isempty(h)
h = text('HorizontalAlignment','center');
set(gca,'xlabel',h);
end
set(h,'string',string,'fontname',ht,'fontsize',hs);
<------- YLABEL.M ------->
function ylabel(string)
% YLABEL Y-axis labels for 2-D and 3-D plots.
% YLABEL('text') adds text beside the Y-axis on the current % axis.
%
% See also XLABEL, ZLABEL, TITLE, TEXT.
% Copyright (c) 1984-92 by The MathWorks, Inc.
h = get(gca,'ylabel');
ht = get(text,'fontname');
hs = get(text,'fontsize');
if isempty(h)
h = text;
set(gca,'ylabel',h);
end
set(h,'string',string,'fontname',ht,'fontsize',hs);
<------- ZLABEL.M ------->
function zlabel(string)
% ZLABEL Z-axis labels for 3-D plots.
% ZLABEL('text') adds text above the Z-axis on the current % axis.
%
% See also XLABEL, YLABEL, TITLE, TEXT.
% Copyright (c) 1984-92 by The MathWorks, Inc.
h = get(gca,'zlabel');
ht = get(text,'fontname');
hs = get(text,'fontsize');
if isempty(h)
h = text;
set(gca,'zlabel',h);
end
set(h,'string',string,'fontname',ht,'fontsize',hs);
<------- TITLE.M ------->
function title(string)
% TITLE Titles for 2-D and 3-D plots.
% TITLE('text') adds text at the top of the current axis.
%
% See also XLABEL, YLABEL, ZLABEL, TEXT.
% Copyright (c) 1984-92 by The MathWorks, Inc.
h = get(gca,'title');
ht = get(text,'fontname');
hs = get(text,'fontsize');
if isempty(h)
h = text('horiz','center');
set(gca,'title',h);
end
set(h,'string',string,'fontname',ht,'fontsize',hs);
For more information, see the MATLAB technical note written on this topic. It is located on the ftp site in pub/tech-support/tech-notes/gr2.txt.

2.2.1.1. How do I change the font of text objects?

To change the font name or font size of a text object, do the following
set(h,'FontName','font')
where h is the handle to the text object, and font is the name of the font you wish to use.
For more information, see the MATLAB technical note written on this topic. It is located on the ftp site in pub/tech-support/tech-notes/gr6.txt.

2.2.1.2. How do I change the font of tick labels?

Although undocumented, all the standard text object font properties such as FontName , FontBold , and FontItalic are also properties of axes. For example, typing:
set(gca,'FontStyle','courier')
will change the font of x and y tick labels before they are created. To change the font of existing labels, do the following:
h=get(gca,'Xlabel');
set(h,'FontName',font);
For more information, see the MATLAB technical note written on this topic. It is located on the ftp site in pub/tech-support/tech-notes/gr10.txt.

2.2.1.3. How do I get Greek letters in my text objects?

You can obtain Greek characters from the symbol font. To implement:
h=text(x,y,'string')
set(h,'FontName','courier');
or
set(gca,'FontName','symbol');
text(x,y,setstr(num));
where x and y are the coordinates on the graph where you want to place the Greek symbols, and num is the value from the list of 0-255 characters from the symbol font. To display a table of the symbol font, you can use the following M-file called chart.m.
function chart( fontname )
% CHART Show ANSI-chart like display of characters MATLAB
%can produce.
% CHART( 'fontname' ) will put up a figure
% window with all 255
% characters of the named font in a 16x16 grid.
% Close figure when done, it creates 256 text
% objects. You may want
% that memory back!
% Chuck Packard, The Mathworks, Inc., 25 Jan 93
% This is an unsupported, purely for example, M-
% file.
%
%
% TO USE THIS CHART:
% USE SETSTR( VALUE), WHERE VALUE=( (16*XCOORD)+YCOORD)
%
%
% make a new figure and axis
% (I'm assuming you want to keep the current graph in the
% gcf.)
%
figure;
axis([-1 16 -1 16])
ax = gca;
%
%set font to be used
%
set(ax, 'DefaultTextFontName', fontname )
%
%some other Handle Graphics settings, written out in
%full.
%See manual for more info.
%
set(ax, 'YDir', 'Reverse', 'Box', 'on')
set(ax, 'YTick', 0:15, 'XTick', 0:15)
set(ax, 'DefaultTextHorizontalAlignment', 'Center')
set(ax, 'DefaultTextVerticalAlignment', 'Bottom')
%
%not vectorized like all 'good' MATLAB M-files, but
%easier to understand!
%
x = reshape( 0:255, 16, 16 );
for h=1:16
for v=1:16
text(h-1,v-1,setstr(x(v,h)));
end
end

2.2.1.4. Can I have multiple fonts in one text object?

No, you cannot mix fonts, font styles, or font sizes within a text object. 

0 Comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Top WordPress Themes