matlab:将矢量转换为字符串的单元格数组

Jas*_*n S 5 matlab cell

我确信有办法做到这一点,但我不知道它是什么.

假设我有一个向量

v = [1.02 2.03 3.04];
Run Code Online (Sandbox Code Playgroud)

我想使用每个元素的格式字符串将其转换为单元格数组:

'   %.3f'
Run Code Online (Sandbox Code Playgroud)

(%.3f前3个空格)

我怎样才能做到这一点?我尝试了以下方法,但是我收到了一个错误:

>> f1 = @(x) sprintf('   %.3f',x);
>> cellfun(f1, num2cell(v))
??? Error using ==> cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.
Run Code Online (Sandbox Code Playgroud)

pet*_*hor 6

如错误中所述,只需提供UniformOutputas 参数即可false

cellfun(f1, num2cell(v), 'UniformOutput', false)

ans = 

    '   1.020'    '   2.030'    '   3.040'
Run Code Online (Sandbox Code Playgroud)