How to use Comparison tags inside my class? or any other operators?

1 vue (au cours des 30 derniers jours)
safaa saber
safaa saber le 17 Oct 2021
Modifié(e) : safaa saber le 18 Oct 2021
Hello Every one...
I am new to matlab classes, i write this one to parse data that come after the charcter searching for. when i run this code:
s= ParseData;
b = ParseData;
b.char = 'D';
s.data = 'D123 X12';
parseData(b,s)
this error occur
((Operator '==' is not supported for operands of type 'ParseData'.))
here is my class code:
classdef ParseData
properties
char
data
end
%%%%%%%%%%%%%%%%%%
methods
function parseData(obj1,obj2)
num = '';
for i = 1:length(obj2)
k = i;
if(obj2(i) == obj1)
while 1
k = k + 1;
num = num + obj2(k);
if(obj2(k+1)==' ' || obj2(k+1)=='.' || obj2(k+1)=='-')
break;
end
end
end
end
end
end
end
code any one help with this error pleas?.

Réponse acceptée

Steven Lord
Steven Lord le 17 Oct 2021
You haven't defined what it means to perform the == operator (whose function name is eq) on instances of your class.
Did you instead mean to compare parts of the properties of your objects? For instance did you want to compare part or all of the data property of your objects?
if(obj2.data(i) == obj1.data)
Though for that you'd probably want to ask if any of the elements in obj1.data matched.
if any(obj2.data(i) == obj1.data)
  1 commentaire
safaa saber
safaa saber le 18 Oct 2021
Modifié(e) : safaa saber le 18 Oct 2021
this line is correct:
if (obj2.data(i) == obj1.data)
but i also used strcmp function insted of == operator.
thanks for your help

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Argument Definitions dans Help Center et File Exchange

Produits


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by