Why Using '%f' as formatspec for a text file containing floating numbers gives no output
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Zhou Ci
el 1 de Sept. de 2021
Respondida: KSSV
el 1 de Sept. de 2021
Hello everyone,
I want to read data fom a text file. Text file contains different rows and column. I used below lines of code to read the data but it returns an empty A:
fileID = fopen('Data.txt', 'r');
formatSpec = '%f';
A = fscanf(fileID, formatSpec);
Secondly, I just want values of t where lat and lon equals:
lat = 32.4876 32.4909
lon = 117.5421 117.5739
Thank you.
0 comentarios
Respuesta aceptada
Walter Roberson
el 1 de Sept. de 2021
target_lats = [32.4876 32.4909];
target_lons = [117.5421 117.5739];
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/726444/Data.txt';
A = readtable(filename, 'readvariablenames', true, 'VariableNamingRule', 'preserve')
mask = ismembertol(A.lat, target_lats) & ismembertol(A.lon, target_lons);
selected_t = A.t(mask)
1 comentario
Walter Roberson
el 1 de Sept. de 2021
But to answer your question:
You did not skip any input lines, so your %f was trying to read starting from the very first thing in the file. But the very first thing in the file is the word 'col' which is something that cannot be read with a %f format, so MATLAB stopped reading. fscanf() only continues to read items as long as the format matches, but the word 'col' does not match %f format.
Más respuestas (1)
KSSV
el 1 de Sept. de 2021
data = importdata('data.txt') ;
data = data.data ;
t = data(:,7) ;
x = data(:,17) ;
y = data(:,16) ;
lat = [32.4876 32.4909] ;
lon = [117.5421 117.5739] ;
t_lon = interp1(x,t,lon) ;
t_lat = interp1(y,t,lat) ;
0 comentarios
Ver también
Categorías
Más información sobre Characters and Strings 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!