App Designer Uneditable Code

22 vues (au cours des 30 derniers jours)
Sebastian Caceres
Sebastian Caceres le 12 Jan 2021
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 commentaires
Athrey Ranjith Krishnanunni
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 le 16 Jan 2021
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!

Connectez-vous pour commenter.

Réponse acceptée

Athrey Ranjith Krishnanunni
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.

Plus de réponses (0)

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by