I need help updating the entered_code variable within the functions.

1 次查看(过去 30 天)
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

回答(1 个)

Walter Roberson
Walter Roberson 2024-1-15

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by