gpt4 book ai didi

matlab - fprintf : new line after a certain point

转载 作者:行者123 更新时间:2023-12-02 09:10:47 25 4
gpt4 key购买 nike

请参阅下面的代码。

fileID = fopen('test.txt', 'w');
fprintf(fileID, '{%f, %f}, ', array1(1:end))
fclose(fileID)

我希望输出的格式为:

{1,1}, {2,2}, {3,3}, {4,4},
{5,5}, {6,6}, {7,7}, {8,8},
...

但是,我得到的是:

{1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6}, {7,7}, {8,8}, ...

假设有一个很大的数据集。

我知道我可以在 fprintf 字段中添加“\n”,但这只会在每对值后面添加一个新行。

如何在“x”次“迭代”后的某个点插入新行(在我的例子中,在每第四对值之后)?

最佳答案

为此,您必须为整行创建格式:

nb_pairs = 4;
format = [strjoin(repmat({'{%f, %f},'}, 1, nb_pairs), ' ') '\n'];
% format = '{%f, %f}, {%f, %f}, {%f, %f}, {%f, %f},\n'
fprintf(fileID, format, array1);

关于matlab - fprintf : new line after a certain point,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52497946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com