This video shows the code to set the color brightness of an image using the slider’s value in a MATLAB App. This app is developed in Matlab App Designer R2023a.
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)
% Value changing function: Slider
function SliderValueChanging(app, event)
changingValue = event.Value;
if (isempty(app.a))
app.a = imread(app.Image.ImageSource);
end
app.Image.ImageSource = (app.a)*changingValue;
end
end
Complete source code:
classdef ImageControlApp < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Slider matlab.ui.control.Slider
SliderLabel matlab.ui.control.Label
Image matlab.ui.control.Image
end
properties (Access = private)
a; % Description
end
% Callbacks that handle component events
methods (Access = private)
% Value changing function: Slider
function SliderValueChanging(app, event)
changingValue = event.Value;
if (isempty(app.a))
app.a = imread(app.Image.ImageSource);
end
app.Image.ImageSource = (app.a)*changingValue;
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 = [107 178 427 289];
app.Image.ImageSource = fullfile(pathToMLAPP, 'butterfly.jpg');
% Create SliderLabel
app.SliderLabel = uilabel(app.UIFigure);
app.SliderLabel.HorizontalAlignment = 'right';
app.SliderLabel.Position = [33 88 36 22];
app.SliderLabel.Text = 'Slider';
% Create Slider
app.Slider = uislider(app.UIFigure);
app.Slider.Limits = [0 2];
app.Slider.ValueChangingFcn = createCallbackFcn(app, @SliderValueChanging, true);
app.Slider.Position = [90 97 518 3];
app.Slider.Value = 1;
% 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 = ImageControlApp
% 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: