how to transfer a cell array content into ordinal values?

1 次查看(过去 30 天)
i have a cell array of string content and i want to turn the contents of cell 2 into ordinal values according to the list shown below in 'clinicalVAL.png'
catnames = { 'stage i'; 'stage ia'; 'stage ii'; 'stage iia'; 'stage iib'; 'stage iic'; 'stage iii'; 'stage iiia';'stage iiib'; 'stage iiic'; 'stage iv'; 'stage iva'; 'stage ivb'};
valueset={1;2;3;4};
for i=1:217
B = categorical(Ystg{i}(2),catnames,valueset,'Ordinal',true);
end
I have tried this code but doesn't work and i want to do it programmatically and not manually; is it possible in MATLAB?
ERROR: Error using categorical
Creating an instance of the Abstract class 'categorical' is not allowed.

采纳的回答

chocho
chocho 2017-4-9
编辑:Stephen23 2017-4-9
Hi Friends, I created this code and run it and is worked well, but i wonder if there is another more fast code that can do this job????
for i=1:217
if strcmpi(Ystg{i}(2),'stage i') || strcmpi(Ystg{i}(2),'stage ia')
Stage(i,1) = 1;
else if strcmpi(Ystg{i}(2),'stage ii') || strcmpi(Ystg{i}(2),'stage iia')|| strcmpi(Ystg{i}(2),'stage iib')|| strcmpi(Ystg{i}(2),'stage iic')
Stage(i,1) = 2;
else if strcmpi(Ystg{i}(2),'stage iii') || strcmpi(Ystg{i}(2),'stage iiia')|| strcmpi(Ystg{i}(2),'stage iiib')|| strcmpi(Ystg{i}(2),'stage iiic')
Stage(i,1) = 3;
else if strcmpi(Ystg{i}(2),'stage iv') || strcmpi(Ystg{i}(2),'stage iva')|| strcmpi(Ystg{i}(2),'stage ivb')
Stage(i,1) = 4;
end
end
end
end
end
  1 个评论
Stephen23
Stephen23 2017-4-10
编辑:Stephen23 2017-4-10
@chocho: you accepted your own answer... even though you ask " i wonder if there is another more fast code that can do this job????"
You know that accepting your own answer discourages other people from helping you, because it tells everyone that this problem has been solved. It also prevents other people from gaining reputation points, when their answer is accepted.

请先登录,再进行评论。

更多回答(2 个)

Jan
Jan 2017-4-9
Or simpler:
for i=1:217
switch Ystg{i}(2)
case {'stage i', 'stage ia'}
Stage(i,1) = 1;
case {'stage ii', 'stage iia', 'stage iib', 'stage iic'}
Stage(i,1) = 2;
case {'stage iii', 'stage iiia', 'stage iiib', 'stage iiic'}
Stage(i,1) = 3;
case {'stage iv', 'stage iva', 'stage ivb'}
Stage(i,1) = 4;
otehrwise % No switch without otherwise!
error('Unexpected value!');
end

Stephen23
Stephen23 2017-4-9
编辑:Stephen23 2017-4-9
This is easy with regexp in just four lines, no loops or ifs or switches are required:
>> C = vertcat(Ystg{:});
>> D = strcat('\s(',{'i','ii','iii','iv'},')[a-d]?$');
>> E = cellfun(@(s)regexp(C(:,2),s,'once'),D,'Uni',0);
>> [F,~] = find(~cellfun('isempty',horzcat(E{:})).')
F =
3
3
2
2
2
2
3
2
1
2
1
3
2
1
3
3
3
4
4
2
2
3
3
...etc

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by