This video shows the code for saving an image matrix into a JPG file from a MATLAB App developed in App Designer. It uses IMWRITE command to save the image to the file.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Complete source code and other details:
Main Method:
methods (Access = private)
% Button pushed function: SaveButton
function SaveButtonPushed(app, event)
a = imread (app.Image.ImageSource);
b = a / 2;
FileName = app.FileNameEditField.Value;
imwrite(b, FileName);
end
end
Complete Class:
classdef ImageSaveToFile < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
FileNameEditField matlab.ui.control.EditField
FileNameEditFieldLabel matlab.ui.control.Label
SaveButton matlab.ui.control.Button
Image matlab.ui.control.Image
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: SaveButton
function SaveButtonPushed(app, event)
a = imread (app.Image.ImageSource);
b = a / 2;
FileName = app.FileNameEditField.Value;
imwrite(b, FileName);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Get the file path for locating images
pathToMLAPP = fileparts(mfilename('fullpath'));
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create Image
app.Image = uiimage(app.UIFigure);
app.Image.Position = [177 276 260 178];
app.Image.ImageSource = fullfile(pathToMLAPP, 'flower1.jpg');
% Create SaveButton
app.SaveButton = uibutton(app.UIFigure, 'push');
app.SaveButton.ButtonPushedFcn = createCallbackFcn(app, @SaveButtonPushed, true);
app.SaveButton.Position = [472 195 100 23];
app.SaveButton.Text = 'Save';
% Create FileNameEditFieldLabel
app.FileNameEditFieldLabel = uilabel(app.UIFigure);
app.FileNameEditFieldLabel.HorizontalAlignment = 'right';
app.FileNameEditFieldLabel.Position = [57 195 60 22];
app.FileNameEditFieldLabel.Text = 'File Name';
% Create FileNameEditField
app.FileNameEditField = uieditfield(app.UIFigure, 'text');
app.FileNameEditField.Position = [132 195 292 22];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = ImageSaveToFile
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
Screenshots: