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

3 ビュー (過去 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 Center および File ExchangeArgument Definitions についてさらに検索

タグ

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by