I use MATLAB R2021b. I have a 10x10 data array and I am using contourf function to plot the data. By default, MATLAB gives me a figure with 1120x840 pixels.
I know that I can use the command set(gcf, 'position', [700, 700, 100, 100]) to decrease the size of the figure. And I get a figure with 200x200 pixels. I decided to shrink the value in the set function by half to get a 100x100 figure.
However, when I try to do set(gcf, 'position', [700, 700, 50, 50]) , I got a figure with size 100x160. No matter how much further I decrease the value in the set function, the second dimension of the figure remained at 160 pixels. And there's no problem with the first dimension -- it can shrink with the value in the set function.
Does anyone know what's the problem here? My goal is to get a figure with 100x100 pixels.
Thank you very much for any advice!
MATLAB has always had a minimal size for figure windows. A smaller window cannot contain the minimal set of menus and toolbar buttons.
But you don’t need a small window to export a small plot.
When exporting a plot, MATLAB uses the “PaperPosition” and “PaperUnits” figure properties. Set the units to pixels and the size to your required sizes to export the figure in those sizes.
There are of course other alternatives:
Set the axes to your required sizes, then export only the axes area (or crop the produced image to size).
Export a figure at twice (or four times) the size, then resample by a factor 1/2 (or 1/4).
If you’re not exporting, and just want a small display on the screen, consider putting multiple things in the same figure window, all you need to do is control the axes’s “Position” property.
I am the original poster. I think there is a minimum window size in both Windows and Mac operating systems. 100 pixels are smaller than the minimum windows size, so I cannot generate such a small figure.
This should be the answer for my question.
Related
I am trying to save a large image (1641x6139) at full resolution with MATLAB with axes. Doing this without axes is easy using the imwrite command. To plot the image with axes, I write:
image(x,y,my_image);
Because the image is very large, MATLAB automatically reduces the resolution of the image. The window size would have to be significantly larger than the screen to display the entire image at full resolution. I have tried a couple different approaches to exporting the figure and each runs into the same problem: the image is pixelated at a coarser resolution than the original image. The problem is not that the new image has fewer pixels; instead, neighboring pixels have been assigned the same value, creating the appearance of coarser-scale pixelation.
For example, where H is the figure handle, I tried:
set(H, 'PaperPosition', [0, 0, width , height ])
print -dpng -r500 my_filename
Increasing width, height, or resolution all had the effect of increasing the number of pixels in the image, but the apparent pixelation is unchanged.
I also tried using the function export_fig, which seems to be designed for this type of problem, claiming to export figures with contained images in their native resolution. The resulting image appears to have the correct number of pixels, but as before the problem is that neighboring pixels have the same value, creating the appearance of coarser pixelation absent from the original image. The code I used is:
export_fig(my_filename,'-a1','-native')
Here are a couple images showing roughly the same zoomed in portion of my image:
The first is the original image and the second is the image resulting from export_fig. The second looks pixelated, although again note that each image contains roughly the same number of actual pixels.
Any advice/solutions would be much appreciated! This problem has been much more frustrating than I expected.
I figured out a simple solution. First, I create a blank image of the correct size, add the axes, and then use export_fig to generate an image of the axes. Then, I write my full-resolution image into the image matrix, and save the resulting image using imwrite.
I thought this problem would be trivial but I haven't been able to figure it out. I have two 3D figures, representing two data sets that are of different physical size (ie. one might represent something 10cm wide, one 20cm wide). I non-dimensionalize everything (so width goes from 0 to 1) and then I use daspect to change the aspect ratio of the plot so that the width appears to change between the plots, even though the axis limits do not (I set these to be constant).
When I plot them, in different figures, however, MATLAB automatically scales the axis to fit in the figure. This means that my objects change size slightly, whilst I want them to keep the x and z scales exactly the same.
Is there some way I can manually set how many pixels one unit of an axis takes up? You can see in the images that the scales on the z axis of the two figures are different, ie. the same length in zaxis units takes up different lengths in pixels.
The code I used for the axes, view, and aspect ratio is:
xlim([-0.3 .6])
ylim([-.8 .8])
zlim([-0.2 1.3])
view([60 15])
daspect([1/268.3 1/(99.9730*width) 1/74])
James
I'm making a GUI (with GUIDE) in which there is an axis used to display image sequences. In order to let the user select a region of interest in the sequence I'm using 'imrect'. The problem is the following: everything goes fine when images are smaller than 512x512 pixels (approximately), however for larger images (I tried 600x600 and 1024x1024) the rectangle does appear, I can change its size but I can't drag it around. I though it had to be with axis units so I changed the property from 'pixels' to 'normalized' and use normalized coordinates, but it does not work.
Here is my code to create the rectangle and restrain its movement to the axis limits:
hROI = imrect(hVideo,[Width/4 Height/4 Width/2 Height/2]; % Arbitrary size and position of the rectangle, centered on the image.
fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim'));
setPositionConstraintFcn(hROI,fcn);
When I perform the same operation on those large images outside the GUI it works. Any hint is welcome!
thanks
I found a workaround to the problem, in case it can help someone:
In the call to imshow just before calling imrect, we need to specify the axis limits as the "XData" and "YData" parameters.
Example:
imshow(Movie{Frame},'parent',handles.axes1_Video,'XData',get(gca,'XLim'),'YData',get(gca,'YLim'))
It works for images up to 1024x1024.
I am trying to draw 256 small sized squares using MATLAB rectangle function. If I am drawing abut 10 squares then the following works fine:
for i=1:2:40
rectangle('Position',[5,3+i,0.3,0.3],...
'Curvature',[0,0],...
'LineStyle','-', 'faceColor', 'black')
end
axis off;
daspect([1,1,1])
But when I change the last value of for loop to 512 (to draw 256 squares), it is not printing properly:
Here is the magnified version of a section of the above image:
This image clearly shows some thing is wrong somewhere as the sides of the squares are not perfectly equal and that the squares are becoming smaller in size for higher no. of squares: Can anybody help me draw the squares perfectly with size not diminishing, ? (I don't have any issues with memory, and I can tolerate multiple pages scrolling down for covering entire squares )
It is a classical Moire effect. You can't show that much rectangles on your monitor, because there aren't enough pixels. Matlab does some down-sampling for you. Thus, you get another frequency, that did not exist originally.
I tried your code and it works fine even when the loop does 512 iterations - when you zoom-in in the final matlab figure. The artifacts you describe are probably caused by the monitor resolution or a low resolution while exporting to non-vector files.
Try to export your image as a vector file (eps or svg) to see that everything looks fine when you zoom in.
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