显示没有引号的单元格数组 - MATLAB

时间:2015-05-14 17:44:04

标签: arrays matlab cell-array

我在Matlab中有一个名为outstr的单元格数组。所有元素都是字符串。当我尝试显示我的数组时,每个字符串都在引号之间,如下所示:

>> outstr

outstr =

'a'          'b'          'Fa'          'Fb'         'Xn'         'Fx'         'sign Fa*Fx'
'0.70000'    '0.90000'    '-0.19592'    '0.33887'    '0.77327'    '0.02896'    '-'         
'0.70000'    '0.77327'    '-0.19592'    '0.02896'    '0.76383'    '0.00206'    '-'         
'0.70000'    '0.76383'    '-0.19592'    '0.00206'    '0.76316'    '0.00012'    '-'         
'0.70000'    '0.76316'    '-0.19592'    '0.00012'    '0.76312'    '0.00000'    '-'      

如何在没有引号的情况下显示我的数组?

3 个答案:

答案 0 :(得分:1)

这样的事情可能会满足需求 -

%// Input
outstr ={
'a'          'b'          'Fa'          'Fb'         'Xn'         'Fx'         'sign Fa*Fx'
'0.70000'    '0.90000'    '-0.19592'    '0.33887'    '0.77327'    '0.02896'    '-'         
'0.70000'    '0.77327'    '-0.19592'    '0.02896'    '0.76383'    '0.00206'    '-'         
'0.70000'    '0.76383'    '-0.19592'    '0.00206'    '0.76316'    '0.00012'    '-'         
'0.70000'    '0.76316'    '-0.19592'    '0.00012'    '0.76312'    '0.00000'    '-'  }

outstr1 = strcat(outstr,{'   '});  %// add whitespace

%// Convert to char array
outstr_char = char(outstr1{:})

%// Get size parameters
[m,n] = size(outstr1)
p = size(outstr_char,2)

%// Reshape + Permute Magic to create a 
%// char array "replica" of input cell array
out = reshape(permute(reshape(outstr_char.',p,m,[]),[1 3 2]),n*p,m).'

%// Display the char array
disp(out)

示例运行 -

>> outstr
outstr = 
    'a'          'b'          'Fa'          'Fb'         'Xn'         'Fx'         'sign Fa*Fx'
    '0.70000'    '0.90000'    '-0.19592'    '0.33887'    '0.77327'    '0.02896'    '-'         
    '0.70000'    '0.77327'    '-0.19592'    '0.02896'    '0.76383'    '0.00206'    '-'         
    '0.70000'    '0.76383'    '-0.19592'    '0.00206'    '0.76316'    '0.00012'    '-'         
    '0.70000'    '0.76316'    '-0.19592'    '0.00012'    '0.76312'    '0.00000'    '-'         
>> disp(out)
a            b            Fa           Fb           Xn           Fx           sign Fa*Fx   
0.70000      0.90000      -0.19592     0.33887      0.77327      0.02896      -            
0.70000      0.77327      -0.19592     0.02896      0.76383      0.00206      -            
0.70000      0.76383      -0.19592     0.00206      0.76316      0.00012      -            
0.70000      0.76316      -0.19592     0.00012      0.76312      0.00000      - 

答案 1 :(得分:1)

假设您希望列之间的间距为3个空格:

G = {};
for k=1:size(outstr, 2)
    G{k} = char(strcat(outstr(:,k), {'   '}));
end

result = cat(2, G{:});

这会迭代列,在每个元素的末尾添加额外的空格并将它们转换为字符数组,然后最终将它们连接在一起。

答案 2 :(得分:1)

这是一个简短的解决方案:

sprintf('%s\n',string(outstr))