I am trying to save the image at an specific size.

Here is the code I am doing:
[wt,f] = cwt(y,'amor',12000);
h = figure();
t = 0:numel(y)-1;
hp = pcolor(t,f,abs(wt));
hp.EdgeColor = 'none';
set(gca,'xtick',[],'ytick',[],'xticklabel',[],'yticklabel',[]);
exportgraphics(gca,'myplot.png','Resolution',300)
I want to save the image at height and width of 224. How do I modify the resulotion value?

 Accepted Answer

The better way is to calculate the appropriate value.
However, you can also do this quick-n-dirty:
[wt,f] = cwt(y,'amor',12000);
h = figure();
t = 0:numel(y)-1;
hp = pcolor(t,f,abs(wt));
hp.EdgeColor = 'none';
set(gca,'xtick',[],'ytick',[],'xticklabel',[],'yticklabel',[]);
fn='myplot.png';R=300;
px_sz=[224 224];
exportgraphics(gca,fn,'Resolution',R);
im=imread(fn);
im=imresize(im,px_sz);
imwrite(im,fn);
Alternatively, you could also determine with what value you need to scale R to get your intended size in pixels. Note that this does not check if your image is square, so it may stretch your image.

5 Comments

Dirty but effective. it worked. Thanks.
Another thing, if I run
exportgraphics(gca,fn,'Resolution',R);
in a for loop, then I want to first let it save, wait for it while saving, then run
im=imread(fn);
How will I do it?
% something like future, async, await of dart programming language,
% if you know what I am talking about.
By default every Matlab command runs sequentially. The obvious exception is parfor (and other parts of the parallel computing toolbox). The not so obvious exception is the graphics engine, which only refreshes sometimes. You can force a refresh by calling drawnow or using pause with a very small time (e.g. 0.01). If you need it you will need it before the call to exportgraphics. I don't see any indication you would need it after it as well. Matlab will first execute the export, and only then continue with the line of code that reads the freshly stored image.
I would really encourage you to think of a way that ensures the axes has the appropriate size so you can use a known resolution and end up with the correct size image. This way will work, but it is far from efficient, as it writes to the disk as one of its steps. Even an SSD is slow compared to RAM.
Are you aware that pcolor does not display the last row and column? It's easy to prove:
pcolor(magic(3)); % Display a 3x3 matrix.
title('3 x 3 matrix displayed with pcolor()');
As you can see it displays an image of 2x2 pixels, not 3x3 like the image that was passed in to pcolor().
Use imshow() instead of pcolor() and imwrite() to save it to disk, unless you need stuff plotted in the overlay and axes and other annotations (you can use exportgraphics (but still not pcolor) in that case).
I did not know that. Thank you for the info. However, I can not use imshow properly in my code. Would you care to show me how to do it?
[wt,f] = cwt(y,'amor',12000);
h = figure();
t = 0:numel(y)-1;
hp = pcolor(t,f,abs(wt));
hp.EdgeColor = 'none';
set(gca,'xtick',[],'ytick',[],'xticklabel',[],'yticklabel',[]);
fn='myplot.png';R=300;
px_sz=[224 224];
exportgraphics(gca,fn,'Resolution',R);
im=imread(fn);
im=imresize(im,px_sz);
imwrite(im,fn);
imshow() takes an image so not sure what your image is - maybe f or wt. I don't have the Wavelet Toolbox so I can't run your code.

Sign in to comment.

More Answers (0)

Products

Tags

Asked:

on 7 Jan 2021

Commented:

on 8 Jan 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!