'Fill Unknown data with mode in matlab

Let us suppose we have following matlab code which reads csv file, converts text data to the categorical form and prints data :

data =readtable("fruit_data.csv");
data.fruit_name =categorical(data.fruit_name);
data.fruit_subtype=categorical(data.fruit_subtype);
head(data)





8×7 table

    fruit_label    fruit_name    fruit_subtype    mass    width    height    color_score
    ___________    __________    _____________    ____    _____    ______    ___________

         1          apple        granny_smith     192      8.4      7.3         0.55    
         1          apple        granny_smith     180        8      6.8         0.59    
         1          apple        granny_smith     176      7.4      7.2          0.6    
         2          mandarin     mandarin          86      6.2      4.7          0.8    
         2          mandarin     mandarin          84        6      4.6         0.79    
         2          mandarin     mandarin          80      5.8      4.3         0.77    
         2          mandarin     mandarin          80      5.9      4.3         0.81    
         2          mandarin     mandarin          76      5.8        4         0.81    
tail(data)

we get : ans =

 8×7 table

    fruit_label    fruit_name    fruit_subtype    mass    width    height    color_score
    ___________    __________    _____________    ____    _____    ______    ___________

         4           lemon          unknown       116        6      7.5         0.72    
         4           lemon          unknown       118      5.9        8         0.72    
         4           lemon          unknown       120        6      8.4         0.74    
         4           lemon          unknown       116      6.1      8.5         0.71    
         4           lemon          unknown       116      6.3      7.7         0.72    
         4           lemon          unknown       116      5.9      8.1         0.73    
         4           lemon          unknown       152      6.5      8.5         0.72    
         4           lemon          unknown       118      6.1      8.1          0.7  

i want to fill unknown data with the most frequent element (so called mode), like in pandas, i can select only those rows that contains unknown

data(data.fruit_subtype=='unknown',:) 


10×7 table

    fruit_label    fruit_name    fruit_subtype    mass    width    height    color_score
    ___________    __________    _____________    ____    _____    ______    ___________

         4           lemon          unknown       132      5.8      8.7         0.73    
         4           lemon          unknown       130        6      8.2         0.71    
         4           lemon          unknown       116        6      7.5         0.72    
         4           lemon          unknown       118      5.9        8         0.72    
         4           lemon          unknown       120        6      8.4         0.74    
         4           lemon          unknown       116      6.1      8.5         0.71    
         4           lemon          unknown       116      6.3      7.7         0.72    
         4           lemon          unknown       116      5.9      8.1         0.73    
         4           lemon          unknown       152      6.5      8.5         0.72    
         4           lemon          unknown       118      6.1      8.1          0.7    
 but when i am writing following code :
data(data.fruit_subtype=='unknown',:) =mode(data.fruit_subtype)

result is :

Right hand side of an assignment into a table must be another table or a cell array.

i have tried following

data(data.fruit_subtype=='unknown',:) =cell(mode(data.fruit_subtype))

but i have got :

Error using cell
Conversion to cell from categorical is not possible.

please help me how to fix it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source