Creating calculator with a UI and button function doesn't work as intended.

3 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Voss
Voss am 10 Mär. 2025
isequal(data_temp.(1)(i),lvldd.Value)
lvldd.Value is a character vector.
data_temp.(1)(i) is a (scalar) cell array.
Those two things will never be equal - they are of different classes.
data_temp.(1){i}, however, is a character vector, which can be compared to lvldd.Value using isequal.
isequal(data_temp.(1){i},lvldd.Value)
Or, use strcmp, which will work the same on scalar cell array data_temp.(1)(i) as on its character vector contents data_temp.(1){i}.
strcmp(data_temp.(1)(i),lvldd.Value) % either this
strcmp(data_temp.(1){i},lvldd.Value) % or this (both work)
  4 Kommentare
Voss
Voss am 10 Mär. 2025
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!
Voss
Voss am 10 Mär. 2025
Taking advantage of the fact that incrementHand is nested inside balatroCalc (which means that all variables in balatroCalc are accessible in incrementHand), variables handUI, lvldd, addMult, and addChip can be accessed directly inside incrementHand and don't need to be input arguments. This change is shown in the code below.
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());
% 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];
function incrementHand()
idx = strcmp(handUI.Data.(1),lvldd.Value);
handUI.Data{idx,[2 3]} = handUI.Data{idx,[2 3]} + [addMult(idx) addChip(idx)];
end
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Properties finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by