Search code examples
matlabroman-numerals

How do I convert a cell array of strings representing roman numbers to double?


I am trying to convert a cell array of strings into double so that I can use it.

hourNumbers = {'I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII'}

finHourNumbs = cell(1,length(hourNumbers))
for i=1:length(hourNumbers)
finHourNumbs {i} = str2double(hourNumbers{i});
end

I get the below output which I am NOT expecting

finHourNumbs = 

Columns 1 through 9

[NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]

Columns 10 through 12

[NaN]    [NaN]    [NaN]

I am expecting it to contain the strings so that I can display.

Thanks in Advance


Solution

  • You can use containers.Map, and create your own roman2double like this

    hourNumbers = {'I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII'};
    
    roman2double = containers.Map(hourNumbers, 1:12);
    
    finHourNumbs = cell(1,length(hourNumbers));
    for i=1:length(hourNumbers)
        finHourNumbs {i} = roman2double(hourNumbers{i});
    end
    

    or in a shorter form

    hourNumbers = {'I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII'};
    roman2double = containers.Map(hourNumbers, 1:12);
    finHourNumbs = values(roman2double, hourNumbers);