How to write numerical data in rows in a text file ?

조회 수: 4 (최근 30일)
R HULD
R HULD 2019년 10월 25일
댓글: Ajay Kumar 2019년 10월 25일
Hi Team
I am trying to make a text file and store few vector variables in it.
example
a = [1:10]
b = [200:2010]
c= [30:40]
all equal length variables
save ('xyz.txt','a','b','c')
when i do
type xyz.txt
the command window shows me gibberish. All alpha numeric characters and nothing makes sense. How can i store values in text file and read my file for contents without downloading it into an array or any other variable?

채택된 답변

Ajay Kumar
Ajay Kumar 2019년 10월 25일
I would suggest you to use file opening, writing it and closing it as it is more organised and easily understandable.
fid = fopen('xyz.txt','w');
fprintf(fid, '%s\n',num2str(a),num2str(b),num2str(c));
fclose(fid);
  댓글 수: 2
R HULD
R HULD 2019년 10월 25일
편집: R HULD 2019년 10월 25일
Thank you. My code failed but file identifier method worked: does that mean Txt files cannot be saved as dat files or mat file?
Ajay Kumar
Ajay Kumar 2019년 10월 25일
Theoretically you can save txt files using save, but the data will be corrupted and we have to write extra conversions and logics which makes it more complex and is not recommended.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by