Array/Data Strucutre Returns Problem

조회 수: 1 (최근 30일)
Dong-Kyeong
Dong-Kyeong 2011년 11월 27일
I made a data structure as the following:
idols(1).groupname='2pm';
idols(1).nummembers = 6;
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(1).member(2).name = 'junho';
idols(1).member(2).birthyear = 1990;
idols(1).member(2).height = 178;
idols(1).member(2).blood = 'A';
Then
name=idols(1).groupname
/////////////
The result is
name =
2pm
////////////
If I want the result to be the same as the input, i.e. name='2pm' How should I receive or change the value? Assuming that the method of making the data structure is not changed.
PS: It seems to be the case that if I make the data structure differently, it gives '2pm' instead of 2pm.

채택된 답변

Junaid
Junaid 2011년 11월 27일
I think you can't compare two string with == . You will get dimensions mismatch. As for string '2pm' the length = 3 and for 'SNSD' the length =4. You can use string compare function.
let say.
if (strcmp(idols(1).groupname, idols(2).groupname))
fprintf('Group is 2pm, or whatever you want');
end

추가 답변 (1개)

Junaid
Junaid 2011년 11월 27일
As I understand that
if you store
idols(1).groupname='2pm';
and when you type
idols(1).groupname
you get
2pm. But you are interested to get '2pm'. right ? If yes then it can be as simple as you store them with ' ' .
ex.
idols(1).groupname='''2pm''';
then if you type
idols(1).groupname
you will get
'2pm'
  댓글 수: 3
Dong-Kyeong
Dong-Kyeong 2011년 11월 27일
What I want to do is
function []=Func(idols,varargin)
if idols(1).groupname==varargin(1)
disp('Group is 2pm');
end
end
Func(idols,'2pm','SNSD')
Is it possible?
Walter Roberson
Walter Roberson 2011년 11월 27일
No. You cannot compare strings or cell arrays using "==" . To compare strings you should use strcmp() or strcmpi().
if strcmpi(idols(1).groupname, varargin(1))

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by