在matlab中使用repmat时将数据写入文件

时间:2011-08-26 23:53:01

标签: matlab printf

我使用以下方法生成一组数据:

repmat x=5; y=[repmat(x, 1, 5)];
fn=fopen('A.txt', 'w');
fprintf(fn, '%g\t%g\t%g\t%g\t%g\t\r\n', y)

如何设置它,以便我不必像%g\t中那样多次输入repmat

感谢您的任何意见。

2 个答案:

答案 0 :(得分:0)

如果您不希望%g \ t硬编码的数量

,请尝试以下操作即时生成格式字符串
  fstring = '';
repeats=5;

for n=1:repeats
fstring=[fstring,'%g\t'];
end
fstring = [fstring,'\r\n'];

x=5;
y=[repmat(x, 1, repeats)];
fn=fopen('A.txt', 'w');
fprintf(fn, fstring, y)

答案 1 :(得分:0)

你可以这样做:

x=5; y=[repmat(x, 1, 5)];
save('A.txt', 'y' , '-ASCII');