How to gather data from a table with variables?

While coding a calculator app, i stumbled with a problem. One of the variables (let's call it A) is a result from an equation, and after that it is checked if the variable is higher than another variable (let's call it B). If A is lower than B, then A can be used for another equation, but if A is higher than B, then i have to check for a table, with three more variables (let's call it X, Y and Z), and the result on the table must be used in the same said equation. The variables X, Y and Z are gathered from equations, edit fields, or button group, so there's the big problem. In order to know wich number of the table i will use for the next equation, the table itself has to check for different variables. I'm currently using only 2 callbacks in the same function, here is an example of what my problem is.
Example:
X will be written at an edit field.
Y is a result of an equation that uses another equations results, but to simplify the exaxple i just used a simple one.
Z is a gathered from a Button Group, it is used for giving C a value, and also as a parameter on the table, so i need the table to aknowledge what button was pushed at the group (Z1 or Z2).
So as i said, A has to be lower than B, or else A will be selected in the table, following the right parameters.
% Callback function: CalculateButton, ZButtonGroup
function CalculateButtonPushed(app, event)
% Z button group
if app.Z1Button.Value
C=1;
elseif app.Z2Button.Value
C=2;
end
B=C*10;
d=app.dEditField.Value;
E=app.EEditField.Value;
X=app.EEditField.Value;
Y=d*X;
A=d*E;
if A<=B
R=A*100;
elseif A>B
R=app.UITable(?)*100
end
note: i don't know how to make a code that gather all information i gave, and auto select the right number from the table.
note2: i will probably have to input that table data into codes aswell, so if that's the case, i accept some enlightment on how to set the table.

 Accepted Answer

Have the button callback set an application variable when the user selects it; then use that value in the calculate callback function. Then use an expression to compute the proper column in the table by values of the Z and X flag variables.
s=sprintf('X Z ix\n');
for X1=1:2
for Z=1:2
app.Z=Z;
ixCol=2*(X1>1)+(app.Z>1)+2; % table column lookup (+2 is offset for one-based indexing plus LH column)
s=[s;sprintf('%d %d %d\n',X1,app.Z,ixCol)];
end
end
fprintf('%s',s.')
X Z ix 1 1 1 1 2 2 2 1 3 2 2 4

2 Comments

ADDENDUM:
NOTA BENE: the "+1" above needs to be "+2" -- one to account for one-based indexing in MATLAB arrays plus one for the LH row ID column in the table.

Sign in to comment.

More Answers (0)

Products

Release

R2023a

Asked:

on 21 May 2023

Commented:

dpb
on 22 May 2023

Community Treasure Hunt

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

Start Hunting!