I need help updating the entered_code variable within the functions.
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Walter Roberson
on 15 Jan 2024
entered_code is local to each of the functions.
2 Comments
Walter Roberson
on 16 Jan 2024
The link describes a number of different ways for sharing data between callbacks; use one of them.
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!