0

I would like to convert a numeric array like this:

[12.3134 25.3234 34.4190 466.6765 55.5454]

Into a cell array of strings, with a single digit after the decimal point like this:

'12.3' '25.3' '34.6' '466.6' '55.5'

This arrayfun gets me close, but I can't figure out how to add the format spec to the num2str function.

arrayfun(@num2str, A, 'UniformOutput', false)

2 Answers 2

1

You can pass inputs to anonymous functions:

A = [12.3134 25.3234 34.4190 466.6765 55.5454];
B = arrayfun(@(x)num2str(x, '%.1f'), A, 'UniformOutput', false);

Which returns:

B =

  1×5 cell array

    '12.3'    '25.3'    '34.4'    '466.7'    '55.5'
Sign up to request clarification or add additional context in comments.

Comments

0

Starting in 16b you can do

>> A = [12.3134 25.3234 34.4190 466.6765 55.5454];
>> B = compose('%.1f',A)

B = 

  1×5 string array

    "12.3"    "25.3"    "34.4"    "466.7"    "55.5"

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.