필터 지우기
필터 지우기

How to print a text file in MATLAB?

조회 수: 48 (최근 30일)
Phan
Phan 2014년 8월 21일
댓글: Geoff Hayes 2014년 8월 21일
Dear everyone,
Please help me to solve this problem. I have 2 array and a cell like this:
State=[1 2 3];
Angle=[25 20 13];
Name={'ABC-820' 'S815' 'EG813'};
I want to create a text file like this:
State Name Angle
1 ABC-820 25
2 S815 20
3 EG813 13
Can anyone help me to write a code to make that file?
Thank you so much!

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 8월 21일
Phan - since you want to create a text file, then use fopen as
fid = fopen('myData.txt','w');
where fid is the file descriptor. If it is positive, then you can write data to file
if fid > 0
% do stuff
% close file
fclose(fid);
end
Since the file has been opened, once we have finished with it, we need to close it with fclose. That leaves the do stuff part of the above code - this is where you write out the data line by line
% write the header (tab-delimited columns)
fprintf(fid,'State\tName\tAngle\n');
% write each row
for k=1:length(State)
fprintf(fid,'%d\t%s\t%d\n',State(k),Name{k},Angle(k));
end
Put it together, and give it a go.
----------------------------
An alternative, if you have a more recent version of MATLAB, is to convert the data to a table and then just write the table to file with writetable
tableData = table(State',Name',Angle','VariableNames',{'State','Name','Angle'})
writetable(tableData,'myTableData.txt','Delimiter','\t');
Try either of the above options and see what happens!
  댓글 수: 4
Phan
Phan 2014년 8월 21일
Oh, I did it!
Thank you so so much!!!
Geoff Hayes
Geoff Hayes 2014년 8월 21일
Glad that it worked!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by