write a matrix to a .txt file

조회 수: 3 (최근 30일)
Usman Siddique
Usman Siddique 2018년 10월 16일
댓글: madhan ravi 2018년 10월 17일
Hello;
I am trying to write the data of two column vectors "G" and "O" into two separate .txt files named "inputs.txt" and "outputs.txt" respectively. However I am unable to do so. Both column vectors are of length 994*1. I open the .txt files but they appear empty.
Do I need to create the .txt files "inputs" and "outputs" in the same directory or will MATLAB create them automatically? The files should be updated with new data of "G" and "O" each time I run the program with the existing file data being erased.
Any help in this query will be highly appreciated.
Thanks
feild1=fopen('inputs.txt','w');
fprintf(fileid1,'%d \r\n',G);
type inputs.txt
fclose(fileid1);
fileid2=fopen('outputs.txt','w');
fprintf(fileid2,'%d \r\n',O);
type outputs.txt
fclose(fileid2);

채택된 답변

madhan ravi
madhan ravi 2018년 10월 16일
편집: madhan ravi 2018년 10월 16일
dlmwrite('inputs.txt',G) %easy and efficient, assuming datas are only double
dlmwrite('outputs.txt',O)
  댓글 수: 3
Usman Siddique
Usman Siddique 2018년 10월 16일
Thanks, it works perfectly.
can you tell me when I open the file using the default windows notepad the files are unreadable and without spaces. However when I open them using Notepad++ the data appears to be readable.
madhan ravi
madhan ravi 2018년 10월 17일
Sorry I have no idea

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by