MATLAB Answers

How to compare number in Edit Field to number in Ui Table, then checking box?

1 view (last 30 days)
Hi! I am trying to take the value displayed in the numeric edit field " app.Out1"(an output from a simulink model that reads barcodes) and compare it to a column in "app. UITable4" (filled with data from an excel file) to make it check the box in that table (so switching its current logic of FALSE to TRUE if the barcode matches an item in the table.
Below is the current code I have that currently just displays the scanned barcode in the "app.Out1" edit field.
% Button pushed function: ScanButton_2
function ScanButton_2Pushed(app, event)
% opens barcode scanner
simFile = 'demoimaqsl_barcoderecognition.slx';
set(app.edit_SelectedMDLFile2,'Value',simFile);
app.Out2.Tag = 'Out1';
simulinkModel = app.edit_SelectedMDLFile2.Value;
simulinkModel = extractBefore(simulinkModel,'.');
disp('########## Opening Simulink-Model ##########');
fprintf('Simulink-Model: %s.mdl\n', simulinkModel)
open_system(simulinkModel);
set_param(simulinkModel,'SimulationCommand','start');
figure(app.UIFigure)
% logic to match the barcode column with the output of the barcode
% scanner --> match = checkbox is filled, unmatched = empty
% checkbox
% meant to be the index of all the values the barcode column
% itembarcode = readtable(string(app.UITable4.Data{row,3}(1)), "Sheet", 2, "Range" , 4);
% if app.Out1.Value == itembarcode
% something to check box of the associated item, based on barcode in app.Out1
end
Here is my theoretical attempt/idea to index the column with barcodes. I have yet to workout how I could possibly change the logic of the excel file to check the box associated with that barcode value.
% logic to match the barcode column with the output of the barcode
% scanner --> match = checkbox is filled, unmatched = empty
% checkbox
% meant to be the index of all the values the barcode column
% itembarcode = readtable(string(app.UITable4.Data{row,3}(1)), "Sheet", 2, "Range" , 4);
% if app.Out1.Value == itembarcode
% something to check box of the associated item, based on barcode in app.Out1
  1 Comment
Shalaka Kollerkandy
Shalaka Kollerkandy on 15 Jan 2021
Update: I tried something a bit different and would appreciate feedback on this. I am mainly having trouble with line where the row of the checkbox column is attempting to switch the table logic to TRUE in order to check the box.
% table data and scanned barcode variables
UITABLE4DATA = app.UITable4.Data;
barcodescanned = app.Out2.Value;
% finds row index of the matching barcode
rowindex = strcmpi(UITABLE4DATA(:,4), barcodescanned);
% updates column 1 with the checkboxes to check the associated drug off
UITABLE4DATA(rowindex,1) = {1} ; % <- having trouble here bc logic to checkbox
% updates the table itself
app.UITable4.Data = UITABLE4DATA;

Sign in to comment.

Accepted Answer

Shalaka Kollerkandy
Shalaka Kollerkandy on 15 Feb 2021
pause(11);
% 4th column table data and scanned barcode variables
UITABLE4BARCODEDATA = app.UITable4.Data{:,4};
barcodescanned = app.Out2.Value;
%initialized row index and cellvalue variables
cellval = 0;
% finds dimensions of tables rows and columns
[numRows,numCols] = size(UITABLE4BARCODEDATA)
% for loop to go through each row of the 4th column of the table
for r = 1:numRows %-1
cellval = sscanf(string(UITABLE4BARCODEDATA{r,1}),'%li');
if(cellval == int64(barcodescanned))
app.UITable4.Data(r,1) = {true};
end
end
TF = cellfun(@(x) all(x),table2cell(app.UITable4.Data(:,1)));
if (numel(TF(TF == false)) > 0) % if any elements failed and set your flag
set(app.ItemSelectionCompleteButton, 'Enable', 'off')
else
set(app.ItemSelectionCompleteButton, 'Enable', 'on')
end
end

More Answers (1)

Reshma Nerella
Reshma Nerella on 15 Feb 2021
Hi,
In this line of code in the updated code
UITABLE4DATA(rowindex,1) = {1} ;
It replaces the content of cell with value '1'.
If you want to check/uncheck the checkbox, use
UITABLE4DATA(rowindex,1) = {true} ; % for checking
UITABLE4DATA(rowindex,1) = {false} ; % for unchecking
Hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by