I need help updating the entered_code variable within the functions.

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 日

0 投票

2 件のコメント

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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeInteractive Control and Callbacks についてさらに検索

製品

リリース

R2023b

タグ

質問済み:

2024 年 1 月 15 日

コメント済み:

2024 年 1 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by