Go to the first, previous, next, last section, table of contents.


4 Graphics

Q4.1: How do I adjust the fontsize of a ticklabel?

The ticklabel gets its properties from the axis to which it is attached. So set(gca, 'fontsize', 14) should do the trick. Type get(gca) to see what else you can set.

Q4.2: Can I create a pi/sigma/superscript in my ticklabels?

Not directly... MATLAB does not interpret TeX strings in ticklabels. You can play games with placing text by hand. The following MathWorks solutions may give you some guidance:

http://www.mathworks.com/support/solutions/data/27450.shtml

and

http://www.mathworks.com/support/solutions/data/5375.shtml

There are also some free third-party software packages you can use to accomplish this.

Doug Schwarz has written a Styled Text Toolbox that does this. It is freely available at: http://home.earthlink.net/~dschwarz2/stextfun/index.html

Q4.3: Can I open multiple files using uigetfile?

No, but there is file called `uigetfiles.dll' on The MathWorks web site that will allow you to do this, at least on a Windows platform. It can be found on this page: http://www.mathworks.com/matlabcentral/fileexchange/Files.jsp?type=category&id=&fileId=331

Q4.4: I want to use a scrollbar to scroll my edit boxes/buttons

Sorry, there's no easy solution. MATLAB does not support hierarchical figures, so you can't have a container control holding your controls. If you really need this you'll have to create your own, using the callbacks from the scrollbar to modify the position properties of your controls.

Ghassan Hamarneh writes:

What I would do is to add 2 pushbuttons to the figure: one at the top right and another at the bottom right and use these buttons to control the vertical scroll of the content of the figure. (surely you can add another 2 horizontal pushbuttons, lower left and lower right).

Whenever any of these buttons is pressed, you loop over all the controls except the two pushbuttons, and increment/decrement the vertical/horizontal postition value of each control. Something like this:

  % fig_handle is the handle of the figure containing the UI controls
  % pushb1_handle, pushb2_handle  are the handles of the pushbuttons
  % described above

  % find handles of all the controls
  all_handles=findobj(fig_handle);

  % exclude the handles of the 2 pushbuttons and the figure itself
  move_handles=setdiff(all_handles, ...
                       [fig_handle, pushb1_handle, pushb2_handle]);

  % loop over the remaining handles and change their positions
  for k=1:length(move_handles)
    set(move_handles(k),'position', ...
                      (get(move_handles(k),'position'))-[0 0  0 10]);
  end

Q4.5: How can I rotate ticklabels?

You can't do that directly. Use the following Mathworks solutions to place the labels manually as text objects (this is a hack):

http://www.mathworks.com/support/solutions/data/5375.shtml

Q4.6: How can I display data in a grid like Excel?

If you simply want to edit the matrix as if it were an Excel spreadsheet, you can use the builtin array editor in MATLAB 6. Type openvar(my_var) at the command prompt, or double click on the variable in the workspace browser. This editor is limited to 2-D matrices of less than 10000 elements.

If you are working on a PC, an ActiveX object is the way to go. Try searching the archive for "activex" and "grid". Michael Robbins has posted a number of articles on this topic, so you might try adding his name to the search. Try a search like this:

http://groups.google.com/advanced_group_search?as_ugroup=comp.soft-sys.matlab&as_uauthors=robbins&as_q=activex%20grid

Q4.7: How can I write a title or text with multiple lines?

This can be done using a cell array of strings:

      title({'First line','Second Line'})
      text(0.5,0.5,{'First line','Second Line'})

Q4.8: How can I use a common color scale among several images?

One way to do this is to insert a CAXIS command in your plotting function/script. For this to work well, you first need to have a first look at all of your data to determine what are the minimum and maximum values over the entire set of images. For example, if the overall minimum value amongst all images is 40 and the overall maximum is 256, you may issue the command "caxis([40 260])" for each image.

Tech note 1215, at http://www.mathworks.com/support/tech-notes/1200/1215.shtml, addresses a related question, namely "How can I use multiple colormaps in a single figure". As a bonus, it includes a thorough discussion of colormaps in general.

Q4.9: How can I set default handle graphics properties?

There are probably several hundred of these default handle graphics options. Rather than trying to remember any particular one, the best thing is to learn the general principle behind all of these default handle graphics properties. The basic call to insert into your startup.m file is :

      set(0,'DefaultObjectnamePropertyName',Value)

For line objects, here are a few examples:

set(0,'DefaultLineMarkerSize',12);
set(0,'DefaultLineMarker','d');
set(0,'DefaultLineLineWidth', 2);

Similarly, you can use these statements for axes objects:

set(0,'DefaultAxesLineWidth', 2);
set(0,'DefaultAxesXGrid','on');
set(0,'DefaultAxesTickDir','out');
set(0,'DefaultAxesTickLength',[0.015 0.015]);
set(0,'DefaultAxesFontName','Arial')

For more details, do a full text search for 'Defining Default Values' in the R12 online help, and click on the very first hit. Also see the following entries in the R12 online help:

Q4.10: How can I modify the default background color used for histograms?

A histogram is made up of patch objects. The trick is to modify the FaceColor property of these patches. A short example follows:

x=rand(400,1);
hist(x);                           % Default facecolor is blue
h=get(gca,'Children');
set(h,'FaceColor', 'm');           % magenta facecolor

% Draw only the edges of the bars making up the histogram
set(h,'FaceColor', 'none');  

Q4.11: How can I draw more than two lines with plotyy?

You can use the axes' handles to plot additional lines, as follows:

x1 = (1:10)';  x2 = x1;  y1 = x1; y2 = x1.^2;
%Plot one line against each of the left and right y-axis
[ax, h1, h2] = plotyy(x1,y1,x2,y2);

%Plot additional line against the left y-axis
x3= x1;  y3 = y1 + 3;
h3 = line(x3,y3,'Parent', ax(1), 'Color',get(h1,'Color'));

%Plot additional line against the right y-axis
x4= x1;  y4 = y2+3;
h4 = line(x4,y4,'Parent', ax(2), 'Color',get(h2,'Color'));
set(ax,'YLimMode','auto','YTickMode','auto')

Q4.12: Is there a command available for plotting an arrow on a graph?

A user-contributed m-file (arrow.m) is available at http://www.mathworks.com/matlabcentral/fileexchange/Files.jsp?type=category&id=&fileId=278

Also look at arrow3.m at http://www.mathworks.com/matlabcentral/fileexchange/Files.jsp?type=category&id=36&fileId=1430

Ned Gulley of The MathWorks has written a review of both of these utilities at http://www.mathworks.com/matlabcentral/spotlight/arrows.shtml

Q4.13: Why does movie(M,1) display the movie M twice?

Quoting from the Matlab 6.1 online helpdesk: The movie function displays each frame as it loads the data into memory, and then plays the movie. This eliminates long delays with a blank screen when you load a memory-intensive movie. The movie's load cycle is not considered one of the movie repetitions.

Q4.14: How can I set the focus in my GUI?

You can't. One hopes that The MathWorks will include this often-requested feature in a future, but there is no guarantee.

Related to this, changing the stacking order of your GUI elements might allow you to set the tab order, but this seems to not always work. GUIDE in MATLAB version >= 6.5 includes a Tab Order Editor, which does a better job at this.

Q4.15: How can I add text labels to data points?

The text command can be used in a vectorized form to automatically add text labels wherever needed. Say you have a matrix D, where the first column contains X coordinates and the second column contains Y coordinates. Then us illustrates:

plot(D(:,1),D(:,2),'+-');
n=num2str(D,'%5.3f/');
n=n(:,1:end-1);          % Just to remove the trailing slash
text(D(:,1),D(:,2),n);


Go to the first, previous, next, last section, table of contents.