Save axes as jpg file in app designer
조회 수: 12 (최근 30일)
이전 댓글 표시
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')
댓글 수: 0
채택된 답변
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
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
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.
SOURCE and FULL Credit: https://www.mathworks.com/matlabcentral/answers/362358-how-do-i-take-a-screenshot-using-matlab
% 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')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!