Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB subplot title and axes labels

I have the following script to ultimately plot a 4 by 2 subplot:

files = getAllFiles('preliminaries');

n = size(files);
cases = cell(1, n);
m = cell(1, n);

for i = 1:1:n
    S = load(files{i});

    cases{i} = retransmission_distribution(S);

    c = size(cases{i});
    m{1,i} = cell(1, c(2));

    %figure(i);
    str_size = size(files{i});
    title_str = files{i}(5:str_size(2) - 4);
    title_str = strrep(title_str, '_', ' ');
    %title(title_str);
    for j = 1:1:c(2)
        [x, y] = hist(cases{i}{1,j});
        m{1,i}{1,j} = [x; int32(y)];
        %  subplot(4, 2, j);
        %  xlabel('Number of Retransmissions');
        %  ylabel('Number of Occurrences');
        %  bar(y, x, 'histc');
    end
end

However, with the current order of command sequence I have, even with them uncommented, the title and axis labels were present for a time before being erased. I want the figure to have its own title, with each subplot having its own axis labels. What's the easiest way to fix it?

like image 386
stanigator Avatar asked Aug 10 '10 23:08

stanigator


1 Answers

For the axis labels, Matt is correct about them having to be placed after the call to BAR. That will take care of one axis label problem. However, you'll likely notice that your y-axis labels in particular may end up being written over one another if they are too long. You have a couple of options to fix this. First, you can adjust the font size in your call to YLABEL:

ylabel('Number of Occurrences','FontSize',7);

Second, you can convert one long label into a multi-line label by using a cell array of strings instead of just a single string:

ylabel({'Number of' 'Occurrences'});

To add a title to the entire figure, the best option is probably to make a UICONTROL static text object and adjust its position so it is placed near the top of the figure. You can get the size and the position of the figure first to help you place the text box near the top and center:

figureSize = get(gcf,'Position');
uicontrol('Style','text',...
          'String','My title',...
          'Position',[(figureSize(3)-100)/2 figureSize(4)-25 100 25],...
          'BackgroundColor',get(gcf,'Color'));

This will create a static text box of width 100 pixels and height 25 pixels placed at the center of the top of the figure and with the same background color as the figure.

like image 89
gnovice Avatar answered Oct 14 '22 18:10

gnovice