0% found this document useful (0 votes)
12 views6 pages

Application4 PDF

App4

Uploaded by

ruffy2403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Application4 PDF

App4

Uploaded by

ruffy2403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

classdef Application4 < matlab.apps.

AppBase

% Properties that correspond to app components


properties (Access = public)
UIFigure matlab.ui.Figure
QUITButton matlab.ui.control.Button
RESETButton matlab.ui.control.Button
LEFTButton matlab.ui.control.Button
RIGHTButton matlab.ui.control.Button
DOWNButton matlab.ui.control.Button
UPButton matlab.ui.control.Button
Panel matlab.ui.container.Panel
GridsizeEditField matlab.ui.control.NumericEditField
GridsizeEditFieldLabel matlab.ui.control.Label
NumberofTargetsEditField matlab.ui.control.NumericEditField
NumberofTargetsEditFieldLabel matlab.ui.control.Label
RobotSimulatorLabel matlab.ui.control.Label
UIAxes matlab.ui.control.UIAxes
end

properties (Access = private)


Grid double
Targets double
RobotX double
RobotY double
NumTargetsFound double
end

methods (Access = private)

function updateRobotPosition(app)

% Clear the previous robot position


app.Grid(app.RobotX, app.RobotY) = 0;

% Update the grid with the new robot position


app.Grid(app.RobotX, app.RobotY) = 1;

% Check for target at the new position


if app.Targets(app.RobotX, app.RobotY) == 1
app.NumTargetsFound = app.NumTargetsFound + 1;
app.Targets(app.RobotX, app.RobotY) = 0; % Remove target
disp('Target found!'); % Display message in MATLAB Command Window
end

% Update the display or plot


show_image(app.Grid, app.UIAxes);
title(app.UIAxes, sprintf('Robot Position: (%d, %d) - Targets Found:
%d', app.RobotX, app.RobotY, app.NumTargetsFound));
end

% Other methods...end
end

% Callbacks that handle component events


methods (Access = private)

% Code that executes after component creation


function startupFcn(app)

gridSize = 10; % Set grid size


app.Grid = zeros(gridSize, gridSize);
app.Targets = zeros(gridSize, gridSize);
numTargets = app.NumberofTargetsEditField.Value;
app.Targets(randperm(numel(app.Targets), numTargets)) = 1;
app.RobotX = ceil(gridSize / 2);
app.RobotY = ceil(gridSize / 2);
app.Grid(app.RobotX, app.RobotY) = 1; % Place robot at center
app.NumTargetsFound = 0;
show_image(app.Grid, app.UIAxes);
title(app.UIAxes, sprintf('Robot Position: (%d, %d)',
app.RobotX, app.RobotY));

end

% Button pushed function: UPButton


function UPButtonPushed(app, event)
if app.RobotX < size(app.Grid, 1)
app.RobotX = app.RobotX - 1;
updateRobotPosition(app);
end
end

% Button pushed function: DOWNButton


function DOWNButtonPushed(app, event)

if app.RobotX < size(app.Grid, 1)


app.RobotX = app.RobotX + 1;
updateRobotPosition(app);
end
end

% Button pushed function: RIGHTButton


function RIGHTButtonPushed(app, event)

if app.RobotY < size(app.Grid, 2)


app.RobotY = app.RobotY + 1;
updateRobotPosition(app);
end
end

% Button pushed function: LEFTButton


function LEFTButtonPushed(app, event)
if app.RobotY < size(app.Grid, 2)
app.RobotY = app.RobotY - 1;
updateRobotPosition(app);
end
end

% Button pushed function: QUITButton


function QUITButtonPushed(app, event)

delete(app);

end

% Value changed function: NumberofTargetsEditField


function NumberofTargetsEditFieldValueChanged(app, event)
numTargets = app.NumberofTargetsEditField.Value;
app.Targets = zeros(size(app.Grid)); % Clear existing targets
app.Targets(randperm(numel(app.Targets), numTargets)) = 1; % Set new
targets
updateRobotPosition(app);
end

% Value changed function: GridsizeEditField


function GridsizeEditFieldValueChanged(app, event)
gridSize = app.GridsizeEditField.Value;
app.Grid = zeros(gridSize, gridSize); % Reset grid size
app.Targets = zeros(gridSize, gridSize); % Reset targets
numTargets = app.NumberofTargetsEditField.Value;
app.Targets(randperm(numel(app.Targets), numTargets)) = 1;
app.RobotX = ceil(gridSize / 2);
app.RobotY = ceil(gridSize / 2);
updateRobotPosition(app);
end

% Button pushed function: RESETButton


function RESETButtonPushed(app, event)
% Reset robot's position to the center of the grid
gridSize = size(app.Grid, 1); % Assuming the grid is square
app.RobotX = ceil(gridSize / 2);
app.RobotY = ceil(gridSize / 2);

% Clear existing targets and reset target positions


numTargets = app.NumberofTargetsEditField.Value; % Assuming this is the
desired number of targets
app.Targets = zeros(gridSize, gridSize);
app.Targets(randperm(numel(app.Targets), numTargets)) = 1;

% Reset the number of targets found


app.NumTargetsFound = 0;

% Clear the grid and update with the robot's new position
app.Grid = zeros(gridSize, gridSize);
app.Grid(app.RobotX, app.RobotY) = 1; % Place robot at new position

% Update the display or plot


updateRobotPosition(app); % If this method updates the UIAxes and title,
reuse it here

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 UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [184 143 274 172];

% Create RobotSimulatorLabel
app.RobotSimulatorLabel = uilabel(app.UIFigure);
app.RobotSimulatorLabel.FontWeight = 'bold';
app.RobotSimulatorLabel.Position = [246 445 159 22];
app.RobotSimulatorLabel.Text = 'Robot Simulator';

% Create Panel
app.Panel = uipanel(app.UIFigure);
app.Panel.Title = 'Panel';
app.Panel.Position = [54 350 545 71];

% Create NumberofTargetsEditFieldLabel
app.NumberofTargetsEditFieldLabel = uilabel(app.Panel);
app.NumberofTargetsEditFieldLabel.HorizontalAlignment = 'right';
app.NumberofTargetsEditFieldLabel.Position = [1 10 104 22];
app.NumberofTargetsEditFieldLabel.Text = 'Number of Targets';

% Create NumberofTargetsEditField
app.NumberofTargetsEditField = uieditfield(app.Panel, 'numeric');
app.NumberofTargetsEditField.ValueChangedFcn =
createCallbackFcn(app, @NumberofTargetsEditFieldValueChanged, true);
app.NumberofTargetsEditField.Position = [120 10 100 22];
app.NumberofTargetsEditField.Value = 10;

% Create GridsizeEditFieldLabel
app.GridsizeEditFieldLabel = uilabel(app.Panel);
app.GridsizeEditFieldLabel.HorizontalAlignment = 'right';
app.GridsizeEditFieldLabel.Position = [360 10 52 22];
app.GridsizeEditFieldLabel.Text = 'Grid size';
% Create GridsizeEditField
app.GridsizeEditField = uieditfield(app.Panel, 'numeric');
app.GridsizeEditField.ValueChangedFcn = createCallbackFcn(app,
@GridsizeEditFieldValueChanged, true);
app.GridsizeEditField.Position = [427 10 100 22];
app.GridsizeEditField.Value = 10;

% Create UPButton
app.UPButton = uibutton(app.UIFigure, 'push');
app.UPButton.ButtonPushedFcn = createCallbackFcn(app,
@UPButtonPushed, true);
app.UPButton.Position = [283 83 100 23];
app.UPButton.Text = 'UP';

% Create DOWNButton
app.DOWNButton = uibutton(app.UIFigure, 'push');
app.DOWNButton.ButtonPushedFcn = createCallbackFcn(app,
@DOWNButtonPushed, true);
app.DOWNButton.Position = [283 39 100 22];
app.DOWNButton.Text = 'DOWN';

% Create RIGHTButton
app.RIGHTButton = uibutton(app.UIFigure, 'push');
app.RIGHTButton.ButtonPushedFcn = createCallbackFcn(app,
@RIGHTButtonPushed, true);
app.RIGHTButton.Position = [382 61 100 23];
app.RIGHTButton.Text = 'RIGHT';

% Create LEFTButton
app.LEFTButton = uibutton(app.UIFigure, 'push');
app.LEFTButton.ButtonPushedFcn = createCallbackFcn(app,
@LEFTButtonPushed, true);
app.LEFTButton.Position = [184 61 100 23];
app.LEFTButton.Text = 'LEFT';

% Create RESETButton
app.RESETButton = uibutton(app.UIFigure, 'push');
app.RESETButton.ButtonPushedFcn = createCallbackFcn(app,
@RESETButtonPushed, true);
app.RESETButton.Position = [514 143 100 23];
app.RESETButton.Text = 'RESET';

% Create QUITButton
app.QUITButton = uibutton(app.UIFigure, 'push');
app.QUITButton.ButtonPushedFcn = createCallbackFcn(app,
@QUITButtonPushed, true);
app.QUITButton.Position = [514 61 100 23];
app.QUITButton.Text = 'QUIT';

% 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 = Application4

% Create UIFigure and components


createComponents(app)

% Register the app with App Designer


registerApp(app, app.UIFigure)

% Execute the startup function


runStartupFcn(app, @startupFcn)

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

You might also like