Reading data from file
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Manny
el 9 de Mzo. de 2021
Comentada: Manny
el 9 de Mzo. de 2021
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.
0 comentarios
Respuesta aceptada
Walter Roberson
el 9 de Mzo. de 2021
Editada: Walter Roberson
el 9 de Mzo. de 2021
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Low-Level File I/O en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!