String array and Numeric values

Hi,,
I have a String array that contains Names, I want to allow the user to choose a Name to change either by writing the name as a string OR by writing it's index as a number..
So I wrote this code, but it seems faulty.. Can anyone help with this..
Thanks in advance:
clear all
clc
Nums=[1:5]
%NumsStr=num2str(Nums)
for i=1:5
s=input('s= ','s')
s=strtrim(s);
S{i}=(s);
clc
end
S
for i=1:5
tst=input('edit what ','s')
try
res=strmatch(tst,S,'exact')
if((length(res)~=0) && (length(res)<2))
S{res(1)}=input('TEXT_MODE, New String =','s')
else
%NumRes=strmatch(tst,num2str(Nums))
NumRes=find(Nums==tst)
if((length(NumRes)~=0) && (length(NumRes)<2))
S{NumRes(1)}=input('NUMBER_MODE, New String = ','s')
end
end
catch ME
end
end

 채택된 답변

Laura Proctor
Laura Proctor 2011년 5월 12일

1 개 추천

In your original code, you should change the line after the else statement to:
NumRes=find(Nums==str2num(tst))

댓글 수: 1

Ahmad
Ahmad 2011년 5월 12일
I believe this solved the problem.
So many thanks.

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

추가 답변 (1개)

Matt Fig
Matt Fig 2011년 5월 12일

1 개 추천

% Say your String cell looks like this:
S = {'Ted' 'Jim' 'Nancy'}
% To change by name:
cname = 'ted'; % User wants to change 'Ted'...
cname_new = 'Frank'; % to 'Frank'.
idx = strcmpi(S,cname); % Find 'Ted'
if any(idx)
S{idx} = cname_new; % Change to 'Frank'
end
Now to use an index instead of a name, just let the user enter cname_new and idx.
%
%
EDIT In response to your comment:
for ii =1:5
s = input('s= ','s');
S{ii}=strtrim(s);
clc
end
S
for ii = 1:5
tst = input('Edit what string in array? ','s');
idx = strcmpi(S,tst);
if any(idx)
T = input('TEXT_MODE, Enter New String: ','s');
S{idx} = T;
else
idx = str2double(tst);
if ~isnan(idx)
T = input('NUMERIC_MODE, Enter New String: ','s');
S{idx} = T;
end
end
end
S

댓글 수: 3

Ahmad
Ahmad 2011년 5월 12일
Hello Sir.
Could you please explain the answer a little bit more.
I am sure in some how it is Correct answer. But I didnt fully understand it.
Ahmad
Ahmad 2011년 5월 12일
Particularly I didn't understand the index related part.
SO MANY Thanks Sir.
Ahmad
Ahmad 2011년 5월 12일
Thank you very much Sir.
I was hoping that you in particular will find and answer my question.
Thanks again.

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2011년 5월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by