Reputation: 2342
After having read this question, I wanted to use this code to save my picture as to particular size.
I_c(:,:) = cropped_matrix(i,:,:);
set(I_c, 'PaperUnits', 'inches');
x_width = 7.25;
y_width = 9.125;
set(I_c, 'PaperPosition', [0 0 x_width y_width]);
imshow(I_c);
saveas(I_c,'fig1.pdf');
I_c
represents a 2D matrix (about 40x40) of uint8
's.
However, I get the error:
Error using set Invalid handle
This makes me believe that I can only use this code with figures and not matrices which contain matrices. How would I go about this?
I have looked at the API for print
, as suggested as the first answer of the aforementioned linked question, but it also suggests using set
and 'PaperUnits'
.
Note: This question also looks at this problem but suggests the same solution.
Notes About Crowley's Answer
.jpg
being produced is the one shown below. How would I get rid of all the extra white area through code? colourmap
of the figure being produced, to greyscale when I just use image(ImData)
? And here is how the actual figure appears:
Here is the code that I have put in:
im = image(I_c);
set(gcf,'units','inches','position',[1 2 5 5]);
set(gca,'ydir','normal','units','centimeters','position',[0 0 0.5 0.5].*get(gcf,'position')) ;
filename = strcat('slice',int2str(i),'_','bead',int2str(j),'.jpg');
saveas(im,filename);
Upvotes: 0
Views: 226
Reputation: 2331
Suppose we have matrix I_c
containing values and x
and y
the coordinates so value I_c(ii,jj)
corresponds to x(ii)
and y(jj)
coordinates.
Then:
ImMin=min(min(I_c)); % Find the minimum value in I_c
ImData=I_c-ImMin; % Ensure non-zero values
ImMax=max(max(ImData)); % Find maximum value in ImData
ImData=ImData./ImMax; % Ensure ImData do NOT exceed 1
image(x,y,ImData*[1 1 1]) % plot the image in greyscale
set(gcf,'units','inches','position',[1 2 5 5]) % set active figure properties
set(gca,'ydir','normal','units','inches','position',[0 0 1 1].*get(gcf,'position')) % set active axes properties
export_fig(gcf,'figure-I_c','-png','-nocrop')
'ydir','normal'
parameter change default (1,1) point being in top-left corner to "normal" position in bottom-left corner.
[0 0 1 1].*get(gcf,'position)
will read active figure position (here [1 2 5 5]) and after element-by-element multiplication the [0 0 5 5]
is passed to the position
which causes that axes fit the image.
export_fig
function will create figure-I_c.png
image as it is shown in Matlab figure, if -nocrop
is omitted the possible white space in the edges is cropped out. This function is available from MathWorks' File Exchange.
Upvotes: 1