Save axes as jpg file in app designer

21 views (last 30 days)
Hi there,
Im currently trying to code a save button so that the axes can be saved as a .jpg file. I found the following code:
F = getframe(handles.axes1);
Image = frame2im(F);
imwrite(Image, 'Image.jpg')
The problem is i dont want the file to save as "Image.jpg", but save with the name entered in a text field.
I tried:
F = getframe(handles.axes1);
Image = frame2im(F);
imwrite(Image, app.FileNameEditField.Value,'jpg')

Accepted Answer

Image Analyst
Image Analyst on 23 May 2022
Try
F = getframe(handles.axes1);
displayedImage = frame2im(F); % A screenshot of the entire figure, not just the image.
baseFileName = app.FileNameEditField.String; % String property, not Value I believe (if it's like GUIDE)
% Get file parts
[folder, bn, ext] = fileparts(baseFileName);
if isempty(folder)
folder = pwd;
end
% Change extension to PNG which gives an image with no compression
% artifacts, unlike JPG. Ignore any extension they put in there.
baseFileName = [bn, '.PNG'];
fullFileName = fullfile(folder, baseFileName); % Prepend folder.
fprintf('Writing image file "%s".\n', fullFileName);
imwrite(displayedImage, fullFileName)
  2 Comments
J Wells
J Wells on 23 May 2022
This is brilliant, thankyou. I had to make a small change to get it working but thats it.
The change was just:
baseFileName = num2str(app.FileNameEditField.Value)
Image Analyst
Image Analyst on 23 May 2022
OK, thanks for catching that. It seems a bit consistent. Usually value is a number and String is a character array, at least that's the way it's in GUIDE. I believe it's similar in Microsoft Visual Studio those they use "Text" rather than "String". According to what you say, it seems like they're using Value for everything regardless of what type it is. I haven't started using app designer yet since I mostly work on legacy GUIDE programs or create new programs based on old legacy programs. Plus some functions (like impixelinfo) didn't work in App Designer until this last version.
I'm attaching a generic template App Designer GUI that I'll start using once they force me to start using App Designer.

Sign in to comment.

More Answers (1)

Prajwol Tamrakar
Prajwol Tamrakar on 6 Oct 2023
Edited: Prajwol Tamrakar on 6 Oct 2023
When using App Designer and you do not have the latest version 2020 or later, it is literally IMPOSSIBLE to save an image from the figure (app.UIAxes). It seems easy previously (of course easy when using GUIDE or just simple code, but Not for App Designer 2019a version). I wasted lots of hours trying to find a solution for this. The best solution ever found was - just get the screen short using the following code.
% Take screen capture
robot = java.awt.Robot();
pos = [0 0 400 400]; % [left top width height]
rect = java.awt.Rectangle(pos(1),pos(2),pos(3),pos(4));
cap = robot.createScreenCapture(rect);
% Convert to an RGB image
rgb = typecast(cap.getRGB(0,0,cap.getWidth,cap.getHeight,[],0,cap.getWidth),'uint8');
imgData = zeros(cap.getHeight,cap.getWidth,3,'uint8');
imgData(:,:,1) = reshape(rgb(3:4:end),cap.getWidth,[])';
imgData(:,:,2) = reshape(rgb(2:4:end),cap.getWidth,[])';
imgData(:,:,3) = reshape(rgb(1:4:end),cap.getWidth,[])';
% Show or save to file
imshow(imgData)
imwrite(imgData,'out.png')

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!