Why am i getting "Undefined function 'eq' for input arguments of type 'cell'." error?

19 visualizaciones (últimos 30 días)
Hello, I am new to matlab app designer and I was tasked to program an app that computes for delivery prices. although I am not sure why I am getting this error when i try to run my program. In the pickUpValue, I used Items data to set my Items to a certain value which is the same with dropOffValue.
pickUpValue = app.PickUpDropDown.ItemsData;
dropOffValue = app.DropOffDropDown.ItemsData;
if pickUpValue == '1' && dropOffValue == '1'
totalLocCost = 30;
app.TotalShippingCostTextArea.Value = totalLocCost;
else
totalLocCost = 60;
end

Respuesta aceptada

Benjamin Kraus
Benjamin Kraus el 22 de Feb. de 2022
Are you trying to check the currently selected value, or the list of available values? The ItemsData stores the list of available values, not the currently selected value (which is stored in the Value property). In addition, your ItemsData does not need to be a cell-array.
For example, this will create a drop-down menu with four options, which are numbered 1-4.
% Create a drop-down menu with four pets, and assign them numbers from 1-4.
d = uidropdown('Items',{'Dog','Cat','Fish','Hamster'},'ItemsData',1:4);
% You would normally select a value interactively, but I'll select it
% programmatically for this example.
d.Value = 2; % Select 'Cat'.
% Now operate on the selected value.
value = d.Value;
if (value == 2)
disp('You selected Cat');
end
Alternatively, you can leave the ItemData empty and operate directly on the cell-array of character vectors. However, if that is the case, then you probably want to use either isequal or strcmp or equality with a scalar string:
% Create a drop-down menu with four pets.
d = uidropdown('Items',{'Dog','Cat','Fish','Hamster'});
% You would normally select a value interactively, but I'll select it
% programmatically for this example.
d.Value = 'Cat';
% Now operate on the selected value.
value = d.Value;
% Using strcmp
if strcmp(value, 'Cat')
disp('You selected Cat');
end
% Using strcmpi for case-insensitive match
if strcmpi(value, 'cat')
disp('You selected cat');
end
% Using isequal
if isequal(value, 'Cat')
disp('Your selection equals ''Cat''');
end
% Using equality to a string scalar
if value == "Cat"
disp("Your selection equals the string 'Cat'")
end
  1 comentario
Benjamin Kraus
Benjamin Kraus el 22 de Feb. de 2022
Specifically, I see two issues with the code you posted:
pickUpValue = app.PickUpDropDown.ItemsData;
dropOffValue = app.DropOffDropDown.ItemsData;
if pickUpValue == '1' && dropOffValue == '1'
totalLocCost = 30;
app.TotalShippingCostTextArea.Value = totalLocCost;
else
totalLocCost = 60;
end
  1. You are using ItemsData instead of Value. I suspect what you want in this case is Value.
  2. You shouldn't use == (which is the same as the eq function) on character vectors or cell-arays of character vectors.
This is how I would modify your code:
pickUpValue = app.PickUpDropDown.Value;
dropOffValue = app.DropOffDropDown.Value;
if pickUpValue == "1" && dropOffValue == "1"
totalLocCost = 30;
app.TotalShippingCostTextArea.Value = totalLocCost;
else
totalLocCost = 60;
end
But, a better approach would be to populate your ItemData with a numeric vector, then you can use == with the numbers (see my original answer).

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by