Search code examples
arraysmatlabchardoublecell-array

MATLAB - Convert char to 1x5 cell


I have 5 grades F, F, D, B, A in grade = FFDBA . I am not sure how to convert each grade to a cell array? i.e. ans= [F] [F] [D] [B] [A]. cellstr(grade) only creates a 1x1 cell array [FFDBA]. I'd like a 1x5 cell.


Solution

  • Use the num2cell function to convert an array to a cell, when you want to split the array into same-size parts.

    grade = 'FFDBA';
    num2cell(grade,size(grade))
    
    ans = 
        'F'    'F'    'D'    'B'    'A'