필터 지우기
필터 지우기

Export data to .txt file using writematrix function

조회 수: 3 (최근 30일)
Adam Bosco
Adam Bosco 2020년 4월 16일
답변: Tanisha Gosain 2020년 6월 19일
I am using the writematrix(A,'myData.txt','Delimiter','tab') function to export data to a txt file. I need the delimiter but i also need the txt to start at row 5 to avoid the header. I know that for xls you can add 'Range','A5' to specify where the data starts but how do you do that with a txt file?

답변 (1개)

Tanisha Gosain
Tanisha Gosain 2020년 6월 19일
Hi, the 'Range' parameter in writematrix only works for excel files. You can read more about how to specify range while writing to an excel file here.
You can do the following to export data to .txt file:
A = magic(8);
numRows = 4;
%skipping first 4 rows
fid = fopen('myData.txt','w');
for i = 1:numRows
fprintf(fid, '%s\n','');
end
%making format specifications
[r,c] = size(A);
s2 = '';
s1 = '%d\t';
for i = 1:c
s2 = strcat(s2,s1);
end
%writing A to myData.txt
for iter = 1:r
s = strcat(s2,'\n');
fprintf(fid,s,A(iter,:));
end
fclose(fid);
You can read more about fprintf and how to use it here.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by