필터 지우기
필터 지우기

I need help updating the entered_code variable within the functions.

조회 수: 1 (최근 30일)
Andrew
Andrew 2024년 1월 15일
댓글: Walter Roberson 2024년 1월 16일
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일
  댓글 수: 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.

댓글을 달려면 로그인하십시오.

카테고리

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