ali*_*azi 4 matlab printf file
如何将下面脚本中生成的信息(包括以下格式的字符串和表格)保存到文本文件?
格式:
M=5
1 21
2 22
3 23
4 24
5 25
5points
Run Code Online (Sandbox Code Playgroud)
脚本:
clc;
clear all;
close all;
ElmentsNum = "EM=5";
x = (1:5)';
y =(21:25)';
t = table(x,y);
M = "5points"
fileID = fopen('E:/Data.txt');
fprintf(fileID,'%s/n',ElmentsNum)
fprintf(fileID,'%.4f',t)
fprintf(fileID,'%s/n',M)
fclose(fileID)
Run Code Online (Sandbox Code Playgroud)
很简单,只需提取表中的值,然后逐行写入即可:
extracted_table=table2array(t);
fileID = fopen('Data.txt','w');
fprintf(fileID,'%s\n',ElmentsNum);
for ii=1:size(extracted_table,1)
% fprintf(fileID,'%.4f %.4f\n',extracted_table(ii,:)); % if you want decimal places
fprintf(fileID,'%d %d\n',extracted_table(ii,:)); % if you want integers (as in the example)
end
fprintf(fileID,'%s\n',M);
fclose(fileID);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
309 次 |
最近记录: |