Reading data from file

조회 수: 3 (최근 30일)
Manny
Manny 2021년 3월 9일
댓글: Manny 2021년 3월 9일
Hi! I am trying to read in data from a file.
If I have the following file let's call it datafile.m:
1 2 3 4 5 6
1 4 9 16 25 36
20
How would I go about reading each line? I have the following code so far
fid = fopen('datafile.m','r');
x = fscanf(fid, '%d');
%y = fscanf(fid, '%d');
The first line of the data is supposed to represent my x values while the second line of the data is my y values. I also don't want to manually count it (we can see it's length 6) since it's for a part of an assignment and the length of the data can change.

채택된 답변

Walter Roberson
Walter Roberson 2021년 3월 9일
편집: Walter Roberson 2021년 3월 9일
Read one line at a time and sscanf() the line.
fid = fopen('datafile.m','r');
x = sscanf(fgetl(fid), '%d');
Remember to fclose() when you are done.
  댓글 수: 1
Manny
Manny 2021년 3월 9일
Thank you so much!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by