首页 > 解决方案 > 在MATLAB中保存多个txt文件时如何保存带有连续数字的文件名?

问题描述

我有大约 2000 条大小为 7611 * 1 的数据。我想将每个数据保存为 txt 文件。我想把txt文件名保存成1.txt、2.txt、3.txt……格式,怎么办?

另外,我在保存数据的时候,想保持7611*1的格式,怎么办?在这部分中,我将附上我编写的无法正常工作的代码..请帮助。谢谢你。

    st = data which size is 7611*1

    for i = 1:2000

    data = fopen('%d.txt','w',i);   % The next code doesn't work. I wrote this code 
                                    % to tell you what form I want. 

    fprintf(data,'%d \n',st);       % I want to save as a column vector, but when I 
                                    % run it with the code next to it, the broken 
                                    % txt file is saved... I cant find reason..

    end

标签: matlab

解决方案


我建议sprintf在你的 for 循环中使用命令来创建文件名,即

 for i = 1:2000
    fName = sprintf('%0.0f.txt',i)  % create file name
    data = fopen(fName,'w', ... );  % include additional specifications as needed
    fprintf( ... ) %  include specifications as needed
 end

推荐阅读