필터 지우기
필터 지우기

Save axes as jpg file in app designer

조회 수: 12 (최근 30일)
J Wells
J Wells 2022년 5월 23일
편집: Prajwol Tamrakar 2023년 10월 6일
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')

채택된 답변

Image Analyst
Image Analyst 2022년 5월 23일
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
J Wells
J Wells 2022년 5월 23일
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 2022년 5월 23일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Prajwol Tamrakar
Prajwol Tamrakar 2023년 10월 6일
편집: Prajwol Tamrakar 2023년 10월 6일
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')

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by