Filter löschen
Filter löschen

I need help updating the entered_code variable within the functions.

1 Ansicht (letzte 30 Tage)
Andrew
Andrew am 15 Jan. 2024
Kommentiert: Walter Roberson am 16 Jan. 2024
Everything runs fine and when the buttons are pushed, the number displays as intended. However, the entered_code variable doesn't update. I need the array to update so that way the figure closes after 4 numbers and the entered code is compared to the correct code.
clc
clear
%generates and displays a 4 digit code using only 1,2,3,4
correct_code = randi(4,1,4);
disp(code);
%intitializes counter variable
entered_code = [];
%Creates figure and button interface and functions
gui_fig = uifigure('Name', 'Enter the Code', 'Position', [200 200 400 100]);
b1 = uibutton(gui_fig, 'push', 'Position', [0 0 100 100], 'ButtonPushedFcn', @(~,~) one_fcn(entered_code));
b2 = uibutton(gui_fig, 'push', 'Position', [100 0 100 100], 'ButtonPushedFcn', @(~,~) two_fcn(entered_code));
b3 = uibutton(gui_fig, 'push', 'Position', [200 0 100 100], 'ButtonPushedFcn', @(~,~) three_fcn(entered_code));
b4 = uibutton(gui_fig, 'push', 'Position', [300 0 100 100], 'ButtonPushedFcn', @(~,~) four_fcn(entered_code));
%Names Buttons
b1.Text = '1';
b2.Text = '2';
b3.Text = '3';
b4.Text = '4';
b_enter.Text = 'Enter';
%Reads if buttons are pressed
function one_fcn(entered_code)
disp('1')
entered_code(end+1) = 1;
end
function two_fcn(entered_code)
disp('2')
entered_code(end+1) = 2;
end
function three_fcn(entered_code)
disp('3')
entered_code(end+1) = 3;
end
function four_fcn(entered_code)
disp('4')
entered_code(end+1) = 4;
end

Antworten (1)

Walter Roberson
Walter Roberson am 15 Jan. 2024
  2 Kommentare
Andrew
Andrew am 15 Jan. 2024
So how could I go about fixing this?
Walter Roberson
Walter Roberson am 16 Jan. 2024
The link describes a number of different ways for sharing data between callbacks; use one of them.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by