如何将数据保存到MATLAB中的txt文件或excel文件中

时间:2021-08-10 22:19:54

How to save this data (i and a) to a .txt file or .xls file in MATLAB?

如何将这些数据(i和a)保存到MATLAB中的.txt文件或.xls文件中?

for i=1:10
   i
   a=i*2
end

3 个解决方案

#1


4  

Use csvwrite to write coma separated values to a text file. You can read it in Excel, and it is a text file at the same time

使用csvwrite将昏迷分隔值写入文本文件。您可以在Excel中阅读它,它同时也是一个文本文件

i=1:10;
a=i*2;
csvwrite('data.csv', [i; a]);

#2


2  

Matlab provides an file I/O interface similar to that of C: you open a file, output data or formatted text, and close it:

Matlab提供了一个类似于C的文件I/O接口:您打开一个文件、输出数据或格式化文本,并关闭它:

f = fopen( "file.txt", "w" );
for i=1:10,
  a=i*2
  fprintf( f, "%d ", a );
end
fclose( f );  

#3


2  

To save to a text file, there is fprintf, example (from documentation):

要保存到文本文件中,有fprintf示例(来自文档):

x = 0:.1:1;
A = [x; exp(x)];

fileID = fopen('exp.txt','w');
fprintf(fileID,'%6s %12s\n','x','exp(x)');
fprintf(fileID,'%6.2f %12.8f\n',A);
fclose(fileID);

To save to an excel file, there is xlswrite, example (from documentation):

为了保存到excel文件,有xlswrite(来自文档):

filename = 'testdata.xlsx';
A = [12.7, 5.02, -98, 63.9, 0, -.2, 56];
xlswrite(filename,A)

If you do not have excel installed, this will not work. An alternative is then csvwrite, which you later on can easily import in excel (on another pc eg).

如果您没有安装excel,这将不起作用。另一种选择是csvwrite,稍后您可以轻松导入excel(在另一个pc上)。

#1


4  

Use csvwrite to write coma separated values to a text file. You can read it in Excel, and it is a text file at the same time

使用csvwrite将昏迷分隔值写入文本文件。您可以在Excel中阅读它,它同时也是一个文本文件

i=1:10;
a=i*2;
csvwrite('data.csv', [i; a]);

#2


2  

Matlab provides an file I/O interface similar to that of C: you open a file, output data or formatted text, and close it:

Matlab提供了一个类似于C的文件I/O接口:您打开一个文件、输出数据或格式化文本,并关闭它:

f = fopen( "file.txt", "w" );
for i=1:10,
  a=i*2
  fprintf( f, "%d ", a );
end
fclose( f );  

#3


2  

To save to a text file, there is fprintf, example (from documentation):

要保存到文本文件中,有fprintf示例(来自文档):

x = 0:.1:1;
A = [x; exp(x)];

fileID = fopen('exp.txt','w');
fprintf(fileID,'%6s %12s\n','x','exp(x)');
fprintf(fileID,'%6.2f %12.8f\n',A);
fclose(fileID);

To save to an excel file, there is xlswrite, example (from documentation):

为了保存到excel文件,有xlswrite(来自文档):

filename = 'testdata.xlsx';
A = [12.7, 5.02, -98, 63.9, 0, -.2, 56];
xlswrite(filename,A)

If you do not have excel installed, this will not work. An alternative is then csvwrite, which you later on can easily import in excel (on another pc eg).

如果您没有安装excel,这将不起作用。另一种选择是csvwrite,稍后您可以轻松导入excel(在另一个pc上)。