How to increase the size of a subplot figure in MATLAB - matlab

I am trying to create a summary figure that consists of multiple subplots. The issue is that I can not increase the overall size of the figure past the size of the screen. I am trying to create a 12 by 8 subplot so I can view all the figures from an experiment at the same time. However when I generate the subplot this is the result I receive.
Example subplot figure
And here is the code I used to generate said figure.
c = {'r' 'c' 'g' 'm' 'y' 'k'};
for x = 1:96
table = load(file_array(x).name);
table = struct2array(table);
[~,col] = size(table);
subplot_tight(12,8,x);
for t = 1:col
plot(table(:,t), c{t});
hold on
end
H = sprintf('%s\n', file_array(x).name);
title(H);
end
figname = sprintf('%s_Duration_part1',heading.name(1:end-4));
saveas(gcf, figname, 'fig');
close all %reset figure
It's nice that all the sub-figures are visible but it is essentially impossible to actually read the data from the subplots. What would be ideal is to save it so the entire figure is bigger so I can scroll through a bigger figure and look at the sub-figures. Or alternatively if there is a way to save a stack of figures as a slideshow. Ultimately so that all the data for a specific experiment is organized in one respective figure.

If you want to save the whole figure, don't look at the figure itself, but use the print function to configure the renderer.
Try something like:
print('-r1200','Plot','-dpng',)
This sets the DPI setting higher, so you should have more information displayed. You can change some other properties to play with the aspect ratio, the renderer size, etc.
Unfortunately I only have access to octave right now, so I can't really provide you with a working example.

Related

Why is my axis position data changing when I use copyobj in MATLAB?

I'm creating a plot in Matlab with two axes, and I want to copy the entire figure to a smaller window with a specific size, and then save it (this makes it easier to format plots for display in Word documents). The function I'm using is as follows:
function printStandardFigure(figInput,filename)
dpi = 300;
newFig = copyobj(figInput,groot);
newFig.Units = 'inches';
newFig.InnerPosition = [1,1,6.5/2,6.5/2];
print(filename,'-dpng',['-r',num2str(dpi)]);
close(newFig);
end
In theory, this should create a copy of the figure I've made with all its axes, etc. identical to the original but resized to fit the new window size. What actually happens is that the 'Position' property for one of the axes gets reset to [0, 0, 1, 1], but the other one displays just fine. This still happens even when I set the ActivePositionProperty to 'position', which normally causes the axis to hold the Position property constant.
I've also tried using copyobj to copy just the two axes over to the new figure, but I have similar issues with resizing, and this doesn't import any legends even though they are set as children of the axes.
This code works just fine if I only have one axis, so why does the second axis cause it to screw up? I've tried making the original figure have the same aspect ratio as the new one, and I've tried setting their sizes to be exactly equal using the InnerPosition property. I can't figure out what's going on and Matlab's documentation isn't helping.
Here's the original chart
And here's what the output of my function looks like.
Edit: Cris Luengo in the comments suggested I try the subplot command, which I thought was a good idea, so I tried it out. However, the export process is still causing a problem Here are the original and the exported versions of the plot when I do that. The original figure was created to have the same dimensions as the exported plot, just to make sure that resizing the axes wasn't causing the issue. I also get the same problem when I remove lines 6 and 7 from the export function (where I set the size of the new figure).

matlab dynamic scrolling in figures

I've added a number of timeseries to a figures object in Matlab and plotted them as subplots on the graph to display them all together, one after the other. There can be a dynamic number of timeseries plots, I can't know in advance how many there will be.
Unfortunately what seems to be happening is that the figures plot is not expanding to include a scroll bar as new subplots are added. Is there a particular flag I'm not seeing to include this and stop the plots in my figures object from getting smaller and smaller?
As an example:
%Maximise the figure window to full screen
f = figure;
f=gcf;
f.Units='normalized';
f.OuterPosition=[0 0 1 1];
%Add a panel to the figure, we'll add subplots to this
p = uipanel('Parent',f,'BorderType','none');
p.Title = 'Examples';
p.TitlePosition = 'centertop';
p.FontSize = 12;
p.FontWeight = 'bold';
%Add a timeseries subplot to the panel
subplot((length(motifIndexAfterThresholds)*2)+1,1,1, 'Parent',p)
ts0 = timeseries(E4_hrX(startIndex:endIndex),'Name','Parent Motif');
plot(ts0)
title('Parent')
After this point I could add in as many subplots as I like, but the panel is not stretching as I'd have hoped.
If anyone can offer suggestions I'd very much appreciate it.
Anyone with the same problem, check out this (2011) blog post:
https://blogs.mathworks.com/pick/2011/11/23/scrolling-figures-guis/
It turns out that a user submitted function can provide this functionality, and all we need to do is download that .m file, include it in your Matlab path, change the function call from subplot to scrollsubplot in our code.
No idea if this will work long term, a feature like this probably should be included out of the box by matlab itself.

Multiple Subplots with (Sub-)Subplots (MATLAB)

I aim to create 14 subplots with four figures in each subplot. Unfortunately, I do not have any example code to show, as I have not a clue how to go about this. A couple ideas that have popped into my head about how I can go about accomplishing this. One is to create multiple figures separately, then merge them into a single figure. Another is to create subplots with multiple subplots nested inside of them; however, again, I have not a clue how I could go about accomplishing this.
You'll probably find that you are trying to fit too much data onto one figure, and the plots will be too small to see anything of interest. However, a techniques that works, and will give you the option of having individual figures, and combining them into one figure if you wish, is to use individual figures each with a panel on it, then use copyobj to copy to your main figure.
For example,
% Create first figure
hf_sub(1) = figure(1);
hp(1) = uipanel('Parent',hf_sub(1),'Position',[0 0 1 1]);
subplot(2,2,1,'Parent',hp(1));
plot(1:10);
subplot(2,2,2,'Parent',hp(1));
surf(peaks);
subplot(2,2,3,'Parent',hp(1));
membrane;
subplot(2,2,4,'Parent',hp(1));
plot(rand(1,100));
% Create second figure
hf_sub(2) = figure(2);
hp(2) = uipanel('Parent',hf_sub(2),'Position',[0 0 1 1]);
subplot(2,2,1,'Parent',hp(2));
histogram(randn(1,1000));
subplot(2,2,2,'Parent',hp(2));
membrane
subplot(2,2,3,'Parent',hp(2));
surf(peaks)
subplot(2,2,4,'Parent',hp(2));
plot(-(1:10));
% Create combined figure
hf_main = figure(3);
npanels = numel(hp);
hp_sub = nan(1,npanels);
% Copy over the panels
for idx = 1:npanels
hp_sub(idx) = copyobj(hp(idx),hf_main);
set(hp_sub(idx),'Position',[(idx-1)/npanels,0,1/npanels,1]);
end
You may need to be more careful with positioning of the panels, and may want to create the individual figure with their visibility set to off, but the above gives the main idea.

Creating annotation boxes for subplots in a for-loop in Matlab

I have the following code in Matlab that runs through a for loop, reads data from a file and plots 9 different figures, that correspond to some particular "channels" in my data, so I decided to annotate them in the for loop.
clear
clc
for i=1:9
subplot(3,3,i);
hold on
x = [4 13]; % from your example
y = ([1 1]); % from your example
y2 = ([-0.4 -0.4]);
H=area(x,y,'LineStyle','none',...
'FaceColor',[1 0.949019610881805 0.866666674613953]);
H1=area(x,y2,'LineStyle','none',...
'FaceColor',[1 0.949019610881805 0.866666674613953]);
% Create textbox
annotation('textbox',...
[0.719849840255583 0.603626943005185 0.176316293929713 0.308290155440411],...
'String',{'FABLIGHT04','Channel',i},...
'FontWeight','bold',...
'FontSize',10,...
'FontName','Geneva',...
'FitBoxToText','off',...
'EdgeColor','none');
axis([0 24 -0.4 1])
set(gca,'XTick',[0:1:24])
set(gca,'YTick',[-0.4:0.2:1])
xlabel('Time (s)');
end
Initially it was giving me 9 different figures and the annotation thing worked fine. But I wanted to be able to tile them onto a subplot for easier comparison.
Since I switched over to using subplot, it does not annotate my figure properly. On opening the editing dock and generating the code, I find that matlab is plotting everything first and then just putting the annotation boxes in the same figure, one on top of the other. Looking at the code it generated, it apparently takes this part of the code:
annotation('textbox',...
[0.719849840255583 0.603626943005185 0.176316293929713 0.308290155440411],...
'String',{'FABLIGHT04','Channel',i},...
'FontWeight','bold',...
'FontSize',10,...
'FontName','Geneva',...
'FitBoxToText','off',...
'EdgeColor','none');
and does it as:
annotation(figure1,'textbox'...)
etc etc
So for all 9 text boxes, it puts them onto the same figure. I tried to do S=subplot(3,3,i) then annotation(S,'textbox') etc etc, I have also tried S(i)=subplot(3,3,i) and then annotation(S,'textbox') etc etc but nothing seems to work.
I have also tried to change the location of the box. I can't seem to figure out how to make it smaller either.
Does anyone know how to have annotation boxes in the right subplot in a for loop?
Thanks
I'm afraid annotation objects are properties of figures and NOT axes, as such its harder to customize the position of each annotation objects because no matter how many subplots you have, they are all part of the same figure and you need to specify their position relatively to the figure coordinate system.
Therefore, you can manually set the position of each text box in your code depending on the subplot it belongs to...
Simple example:
clear
clc
close all
figure('Units','normalized'); %// new figure window
for k = 1:2
str = sprintf('Subplot %d',k);
subplot(1,2,k)
plot(rand(1,10));
%// Customize position here
hAnnot(k) = annotation('textbox', [k*.4-.2 .6 .1 .1],...
'String', str,'FontSize',14);
end
Which looks like this:
Its not very elegant but I'm personally not aware of any other option if you do need to use annotations objects. A less cumbersome alternative would be to use a simple text objects, which are properties of axes and therefore much more friendly to position :)
Hope that helps!

When saving a figure as eps file, Matlab cuts off colormap labels

I have a figure generated using contourf with a colorbar. Most of my plots are fine, but when the values on the colorbar are of the order 10^{-3}, either the numbers 0.005 etc are written by the colorbar, or x10^{-3} is written at the top.
In both cases, part of the label gets cut off - either the 3 in x10^{-3} or half of the 5 in 0.005.
I can fix this using
set(gca, 'ActivePositionProperty', 'OuterPosition')
for the figure onscreen, but I need to save it in eps format. When I do this, the 3 (or 5) is cut off again!
I can also fix this if I manually pull the bottom right corner of the figure window to make it larger. But this changes the sizes of the axis labels etc in comparison to the plot itself so that they're different to all my other figures, i.e. the figures that I don't resize.
Any suggestions?
Matlab uses two sizes for figures: screen size (Position figure property) and the PaperSize. The former is used for displaying on screen, and the latter for printing or exporting to image formats other than .fig. I suspect this is the source of your problem.
Here is what you can try:
size = get(gcf,'Position');
size = size(3:4); % the last two elements are width and height of the figure
set(gcf,'PaperUnit','points'); % unit for the property PaperSize
set(gcf,'PaperSize',size);
This sets the size of the "paper" to export to .eps to the size of the figure displayed on screen.
If this doesn't work, you can try to play a bit with PaperSize or other "paper" related properties. The Figure Properties documentation page gives more info about properties.
Hope this helps!
The former suggestion is partly correct. Here is what i did:
set both, figure and paper units, to the same measure (figure has pixels, not points!)
set(gcf,'Units','points')
set(gcf,'PaperUnits','points')
do the same as suggested before:
size = get(gcf,'Position');
size = size(3:4);
set(gcf,'PaperSize',size)
the thing now is, that it might be shifted off the paper, as in my case, so put it back on
set(gcf,'PaperPosition',[0,0,size(1),size(2)])
I am not sure about the offset of [0,0], but what is a single point cut off :)
Try this to save your file to filename.eps:
set(gcf,'Units','points')
set(gcf,'PaperUnits','points')
size = get(gcf,'Position');
size = size(3:4);
set(gcf,'PaperSize',size)
set(gcf,'PaperPosition',[0,0,size(1),size(2)])
print(gcf,'filename','-depsc','-loose'); % Save figure as .eps file