Save axes as jpg file in app designer
8 ビュー (過去 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!