Equality error

3 Ansichten (letzte 30 Tage)
Dong-Kyeong
Dong-Kyeong am 27 Nov. 2011
function varargout=practice(varargin)
clc
idols(1).groupname = '2pm';
idols(1).nummembers = 1;
idols(1).gender = 'boys';
idols(1).debut = 2008;
idols(1).member(1).name = 'chansung';
idols(1).member(1).birthyear = 1990;
idols(1).member(1).height = 184;
idols(1).member(1).blood = 'B';
idols(2).groupname = 'SNSD';
idols(2).nummembers = 2;
idols(2).gender = 'girls';
idols(2).debut = 2007;
idols(2).member(1).name = 'taeyeon';
idols(2).member(1).birthyear = 1989;
idols(2).member(1).height = 'NA';
idols(2).member(1).blood = 'O';
idols(2).member(2).name = 'jessica';
idols(2).member(2).birthyear = 1989;
idols(2).member(2).height = 'NA';
idols(2).member(2).blood = 'B';
result=calc_avgage(idols, '2pm', 'SNSD');
disp(result)
end
function out=calc_avgage(idols, varargin)
n=0;
sum=0;
idols(2).member(1).birthyear
for i=1:nargin-1
idols2(i).groupname=char(varargin(i));
end
for i=1:length(idols2)
for j=1:length(idols)
a=idols2(i).groupname;
b=idols(j).groupname;
if a==b
for k=1:idols(j).nummembers
sum=sum+idols(j).member(k).birthyear;
n=n+1;
end
end
end
end
sum
n
out=sum/n;
end
If I run this code, why is it that when a==b, the code runs well
However when a~=b, the Matlab program gives the error
??? Error using ==> eq
Matrix dimensions must agree
How would I fix the problem?

Akzeptierte Antwort

Junaid
Junaid am 27 Nov. 2011
Dear for comparing the string you should use strcmp function.
ex.
a(1).name ='2pm'; a(2).name='SNSD'; a(3).name = '2pm';
strcmp(a(1).name , a(2).name ) % this will return 0
strcmp(a(1).name, a(3).name) % this will return 1.
  1 Kommentar
Dong-Kyeong
Dong-Kyeong am 27 Nov. 2011
thank you! It's working well now!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Quantum Computing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by