Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert numbers to strings in a cell array in MATLAB

I have a cell array with numbers and string data. I need to convert the numbers to strings so that I can use the unique() function.

a = {1; 4; 'lf'}
result --> {'1', '4', 'lf'}; % Now unique() function can be used

There are online solutions to handle a case where the column was numerical. But those cannot be used here as at least 1 row has string as data. A vectorized solution shall be appreciated.

like image 506
Maddy Avatar asked Feb 18 '23 09:02

Maddy


1 Answers

Use cellfun() for applying num2str() to every cell element:

result = cellfun(@num2str, a, 'UniformOutput', false)

This (with UniformOutput set to false) will automatically handle the non-scalar, char elements of the array.

like image 102
gevang Avatar answered Feb 20 '23 20:02

gevang