如何在txt文件中写入一行

时间:2013-12-24 19:27:36

标签: matlab

我已经使用以下代码从Excel中读取了两行:

filename = 'myExample.xlsx';
columnB = xlsread(filename,'B:B')

这就是结果:

 raw = 

第1栏至第4栏

[1]    'J0416'      'J0081 '          [-0.221000000000000]
[2]    'J0081  '    'J0011   '        [ 1.429200000000000]
[3]    'J0011  '    'J0700      '     [-1.713400000000000]
[4]    ' J0700 '    ' J0073   '       [-1.009600000000000]
[5]    ' J0073'     '  M0232     '    [ 1.835500000000000]
[6]    'M0232'      '  M0375   '      [12.486700000000001]

第5栏

[    17.405999999999999]
[1.326780000000000e+002]
[3.917520000000000e+002]
[6.486120000000000e+002]
[8.552070000000000e+002]
[1.185750000000000e+002]

如何将此输入写入如下文本文件?

       distance 1   distance 2
           -0.221       17.406
           1.4292      132.678
          -1.7134      391.752
          -1.0096      648.612
           1.8355      855.207
          12.4867      118.575

1 个答案:

答案 0 :(得分:1)

使用fopen打开文本文件,fprintf写入数据并最终fclose

clear all
A = rand(10,2);
fileId = fopen('out.txt','w');
fprintf(fileId,'%s\t%s\n','distance1','distance2');
fprintf(fileId,'%f\t%f\n',A);
fclose(fileId);