How I convert a str to Num, and label them. please help me.
10 views (last 30 days)
Show older comments
Dear Sir/Madam,
I am a beginner of MATLAB please help me, here is my prog. the below program i want to convert a string into number want a label by connecting diffident string like.
Function num_arr=convert_to_num420(str_cell_arr)
num_arr=zeros(length(str_cell_arr),1);
strs=unique(str_cell_arr);
for i=1:length(str_cell_arr)
for j=1:length(strs)
if strcmp(str_cell_arr{i},'normal.')
num_arr(i)= 1; % upto here prog is fine but next line i got error "like too many parameters"
"sir here i want to convert all the string into num. that is equal to 2" below line.
if strcmp(str_cell_arr{i},'back.','land.','neptune.','pod.','smurf.','teardrop.')
num_arr(i)= 2;
break;
else
num_arr(i)= -1;
end
end
end
end
0 Comments
Accepted Answer
Stephen23
on 17 Sep 2015
Edited: Stephen23
on 17 Sep 2015
Perhaps you are looking for something like this:
function num_arr = convert_to_num420(str_cell_arr)
num_arr = zeros(size(str_cell_arr))-1;
num_arr(strcmp(str_cell_arr,'normal.')) = 1;
X = {'back.','land.','neptune.','pod.','smurf.','teardrop.'};
for k = 1:numel(X)
num_arr(strcmp(str_cell_arr,X{k})) = 2;
end
end
And tested:
>> convert_to_num420({'normal.'})
ans = 1
>> convert_to_num420({'back.','normal.','smurf.','false!'})
ans = 2 1 2 -1
4 Comments
More Answers (1)
Walter Roberson
on 17 Sep 2015
Edited: Walter Roberson
on 17 Sep 2015
if ismember(str_cell_arr{i}, {'back.', 'land.', 'neptune.', 'pod.', 'smurf.', 'teardrop.'})
4 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!