This video shows the steps to write your own function in MAtlab App Designer to convert a number from Decimal to Binary to Gray code in Matlab 2023a.
Details on the Gray code is referred from the wiki page: https://en.wikipedia.org/wiki/Gray_code
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 details:
Main Function:
function ConvertButtonPushed(app, event)
x = app.ImputtheDecimalEditField.Value(1);
binary_value = fi(x, 0,4,0);
gray_code = bitxor(binary_value, bitsrl(binary_value, 1));
app.BinaryCodeEditField.Value = str2double(bin(binary_value));
app.GrayCodeEditField.Value = str2double(bin(gray_code));
end
Complete code:
classdef BinToGrayAppDesigner < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
GrayCodeEditField matlab.ui.control.NumericEditField
GrayCodeEditFieldLabel matlab.ui.control.Label
BinaryCodeEditField matlab.ui.control.NumericEditField
BinaryCodeEditFieldLabel matlab.ui.control.Label
ConvertButton matlab.ui.control.Button
ImputtheDecimalEditField matlab.ui.control.NumericEditField
ImputtheDecimalEditFieldLabel matlab.ui.control.Label
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: ConvertButton
function ConvertButtonPushed(app, event)
x = app.ImputtheDecimalEditField.Value(1);
binary_value = fi(x, 0,4,0);
gray_code = bitxor(binary_value, bitsrl(binary_value, 1));
app.BinaryCodeEditField.Value = str2double(bin(binary_value));
app.GrayCodeEditField.Value = str2double(bin(gray_code));
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% 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 ImputtheDecimalEditFieldLabel
app.ImputtheDecimalEditFieldLabel = uilabel(app.UIFigure);
app.ImputtheDecimalEditFieldLabel.HorizontalAlignment = 'right';
app.ImputtheDecimalEditFieldLabel.Position = [42 410 105 22];
app.ImputtheDecimalEditFieldLabel.Text = 'Imput the Decimal';
% Create ImputtheDecimalEditField
app.ImputtheDecimalEditField = uieditfield(app.UIFigure, 'numeric');
app.ImputtheDecimalEditField.Position = [162 410 100 22];
% Create ConvertButton
app.ConvertButton = uibutton(app.UIFigure, 'push');
app.ConvertButton.ButtonPushedFcn = createCallbackFcn(app, @ConvertButtonPushed, true);
app.ConvertButton.Position = [356 409 100 23];
app.ConvertButton.Text = 'Convert';
% Create BinaryCodeEditFieldLabel
app.BinaryCodeEditFieldLabel = uilabel(app.UIFigure);
app.BinaryCodeEditFieldLabel.HorizontalAlignment = 'right';
app.BinaryCodeEditFieldLabel.Position = [27 297 71 22];
app.BinaryCodeEditFieldLabel.Text = 'Binary Code';
% Create BinaryCodeEditField
app.BinaryCodeEditField = uieditfield(app.UIFigure, 'numeric');
app.BinaryCodeEditField.Position = [113 297 100 22];
% Create GrayCodeEditFieldLabel
app.GrayCodeEditFieldLabel = uilabel(app.UIFigure);
app.GrayCodeEditFieldLabel.HorizontalAlignment = 'right';
app.GrayCodeEditFieldLabel.Position = [386 297 63 22];
app.GrayCodeEditFieldLabel.Text = 'Gray Code';
% Create GrayCodeEditField
app.GrayCodeEditField = uieditfield(app.UIFigure, 'numeric');
app.GrayCodeEditField.Position = [464 297 100 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 = BinToGrayAppDesigner
% 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
Design Screenshots:
Output Screenshots: