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

3 次查看(过去 30 天)
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?.

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

标签

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by