4

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.

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.