MATLAB Answers

App Designer Uneditable Code

65 ビュー (過去 30 日間)
Hello! What I am trying to do is have a 'automated picking list.' In other words, I will have a checklist of item. (I.e. Apple, pear, banana). Each of the items have a specific barcode, 321, 654, and 987 respectively. So, when an item is scanned, I would like it to be automatically 'checked' off the list. Below I put a matlab script with the general idea. I want to implement this logic with the App Designer Toolbox, however I am not able to edit the. Anything with a white background is editable, and a grey background is uneditable. I want to implement the for and if else loops in the .m code to line 49, the value of the checkbox, to change it's toggle instead of print a statement but I cant edit it! any suggestions?
barcode = [321 654 987];
prompt = 'Enter a string of digits to represent a barcode: ';
x = input(prompt);
c = 0;
%For loop will check if scanned item is within list, if so will mark c = 1
for i = 1:length(barcode)
if barcode(i) == x
c = 1;
end
end
%If loop will check item if c = 1
if c == 1
disp('Item Checked!')
else
disp('ERROR: Item Not Required')
end

  6 件のコメント

表示 3 件の古いコメント
Sebastian Caceres
Sebastian Caceres 2021 年 1 月 15 日
Hello! That was very helpful and helped make a lot of progress! I still have one more error after integrating that with the barcode scanner, I've attached it below.
Error:
Unrecognized method, property, or field 'Apple' for
class 'litJustACheckBox'.
Error in litJustACheckBox/ScanButtonPushed (line 84)
app.(checkedBoxName).Value = 1; %
read about dynamic field referencing
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 386)
Error while evaluating Button PrivateButtonPushedFcn.
I've attached a folder with the link below as well if you could look. https://drive.google.com/drive/folders/1Lg0mq3tJ-6D_VAXFVFrjT_BVdmoASrMb?usp=sharing
Athrey Ranjith Krishnanunni
Athrey Ranjith Krishnanunni 2021 年 1 月 16 日
You have to use the actual checkbox names for dynamically referencing them as fields of the object app. In your code, you have modified the property checkBoxNames as:
checkBoxNames = {'Apple', 'Pear', 'Banana', 'Orange'}
so the line
app.(checkedBoxName).Value = 1;
evaluates to
app.Apple.Value = 1;
and app.Apple doesn't exist, which is what the error means.
To fix it, use the full names of the checkboxes (you can see them from Component Browser on the right pane), like I had mentioned in my original code:
checkBoxNames = {'AppleCheckBox','PearCheckBox','BananaCheckBox','OrangeCheckBox'};
Sebastian Caceres
Sebastian Caceres 2021 年 1 月 16 日
Worked perfectly, I'll be sure to read more into dynamic references! If you'd like to copy and paste your answer into an answer instead of comment I'd be happy to accept. If not thats also fine, thanks!

サインインしてコメントする。

採用された回答

Athrey Ranjith Krishnanunni
Athrey Ranjith Krishnanunni 2021 年 1 月 16 日
So basically, the trick here is to find which checkbox's value should be changed when the scanned item matches its corresponding barcode.
OP has said that the scanned item is represented by a number x inside a callback function tied to the scanning process.
So we first save the barcodes of all items in the store as a property of the app, along with the actual full names of the checkboxes they each represent.
properties (Access = private)
barcode = [321 654 987];
checkBoxNames = {'AppleCheckBox','PearCheckBox','BananaCheckBox'};
% in the same order as that of barcode
end
It's important to use the full names of the checkboxes because we'll later use it to refer to the actual checkboxes themselves.
Now, inside the callback function, after x is assigned the barcode of the scanned item, we add the following code:
% see if any entries in barcode are also there in x
isItemChecked = ismember(app.barcode,x);
if ~any(isItemChecked)
disp('ERROR: Item Not Required') % use UIALERT instead to show it on top of your app
return
end
% find names of boxes that are checked
checkedBoxes = app.checkBoxNames(isItemChecked);
% change the value of corresponding checkboxes to 1
for itemNo = 1:numel(checkedBoxes)
checkedBoxName = checkedBoxes{itemNo};
app.(checkedBoxName).Value = 1; % read about dynamic field referencing
end
disp('Item Checked!') % UIALERT like earlier
app.(checkedBoxName) evaluates to app.AppleCheckBox, app.PearCheckBox, etc. depending on context.

  0 件のコメント

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by