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

조회 수: 1 (최근 30일)
safaa saber
safaa saber 2021년 10월 17일
편집: safaa saber 2021년 10월 18일
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