Creating calculator with a UI and button function doesn't work as intended.
Show older comments
Creating a calculator for a game I play for fun. Learning how to use all the MATLAB gui stuff from scratch, so bear with me.
function balatroCalc
hands = {'High Card','Pair','Two Pair','3 of a Kind','Straight','Flush'};
hands = [hands,{'Full House','4 of a Kind','Straight Flush'}];
hands = [hands,{'5 of a Kind','Flush House','Flush 5'}].';
mult = [1,2,2,3,4,4,4,7,8,12,14,16].';
chip = [5,10,20,30,30,35,40,60,100,120,140,160].';
addMult = [1,1,1,2,3,2,2,3,4,3,4,3].';
addChip = [10,15,20,20,30,15,25,30,40,35,40,50].';
data = table(hands,mult,chip);
fig = uifigure("Name","Balatro Calculator");
% Create table
handUI = uitable(fig,"Data",data);
handUI.ColumnName = {'Hands','Mult','Chips'};
handUI.ColumnSortable = false; % temporary
handUI.ColumnEditable = [false true true];
handUI.ColumnWidth = {100,100,100};
% Create increment dropdown
lvldd = uidropdown(fig,"Items",hands);
% Create increment button
lvlb = uibutton(fig,"Push","Text","Planet", ...
"ButtonPushedFcn",@(lvlb,event) incrementHand(handUI,lvldd,addMult,addChip));
% Component positions
% [offset right, offset up, width, height]
x = 20; y = 20; w = 310; l = 310;
fig.Position = [100 100 500 400];
handUI.Position = [x y w l];
lvldd.Position = [335 305 125 25];
lvlb.Position = [335 280 125 25];
% This section SHOULD increment the
function incrementHand(handUI,lvldd,addMult,addChip)
data_temp = get(handUI,'Data');
for i = 1:length(data_temp.(1))
if isequal(data_temp.(1)(i),lvldd.Value)
data_temp.(2)(i) = data_temp.(2)(i) + addMult(i);
data_temp.(3)(i) = data_temp.(3)(i) + addChip(i);
end
end
set(handUI,'Data',data_temp);
end
end
My goal is for incrementHand to add to 2 columns of handUI in the row corresponding to the value the user chooses in a dropdown menu. The code compiles and runs with no error, and a breakpoint + stepping showed me that it enters incrementHand but doesn't ever enter the if statement, which it should do exactly once per button press.
1 Comment
Stephen23
on 10 Mar 2025
Note that rather than using SET and GET on some data hiding inside some object you could simply use nested functions:
Accepted Answer
More Answers (0)
Categories
Find more on Graphics Object Properties 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!