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

1 visualizzazione (ultimi 30 giorni)
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?.

Risposta accettata

Steven Lord
Steven Lord il 17 Ott 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 Commento
safaa saber
safaa saber il 18 Ott 2021
Modificato: safaa saber il 18 Ott 2021
this line is correct:
if (obj2.data(i) == obj1.data)
but i also used strcmp function insted of == operator.
thanks for your help

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Argument Definitions in Help Center e File Exchange

Prodotti


Release

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by