Strange behavior from readtable
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Frank
el 27 de Jun. de 2023
I have some text files that I'm reading in with readtable. For most of the files, everything seems to work fine (example1.txt). Strings come through as strings, and numbers come through as numbers. But for some reason, I'm getting strange results when trying to read in example2.txt. The last 2 columns....which are all numbers....are coming through as strings. I'm thinking there has to be some kind of strange format in the example2.txt file, but I've scanned both files, and don't see anything that would be causing this. My call is simply data = readtable(filename). Obviously I'll try reading in everything as strings to try to fix this, but was just curious as to what could be causing this.
Respuesta aceptada
Stephen23
el 28 de Jun. de 2023
Editada: Stephen23
el 28 de Jun. de 2023
While READTABLE's automagic file format detection is great, the more a file deviates (missing data, lines filled with asterisks) the more help you will have to give it. For this use DETECTIMPORTOPTIONS, SETVARTYPE, etc.
Once it is set up correctly you do not need to repeat this for each file: you can reuse the options object for all files.
F1 = 'example1.txt';
F2 = 'example2.txt';
Ob = detectImportOptions(F1, 'FileType','fixedwidth', 'Range',1, 'TreatAsMissing','-999');
Ob = setvartype(Ob, 'double');
Ob = setvartype(Ob, {'Latitude___','Longitude___'},'char');
Ob = setvartype(Ob, {'FlightTimeSinceLaunch_mins_secs_','UTCTime_hrs_mins_secs_'},'duration');
Ob = setvaropts(Ob, 'FlightTimeSinceLaunch_mins_secs_', 'InputFormat','mm:ss');
T1 = readtable(F1,Ob)
T1{270:279,2}
T2 = readtable(F2,Ob)
Note that you can use CONVERTVARS to efficiently convert the degree+minute+second text into numeric vectors (or scalars), e.g.:
T2 = convertvars(T2,@iscell,@myfun)
Basic matrix multiplication could also be very useful to combine the degrees+minutes+seconds into degrees:
T2.Longitude___ * [1;1/60;1/3600]
function V = myfun(C);
V = nan(numel(C),3);
V(~cellfun(@isempty,C),:) = sscanf([C{:}],'%f°%f''%f"',[3,Inf]).';
end
Thinks out loud: it would be interesting if DURATION accepted a wider range of formats, then degree+minutes+seconds could be natively imported as a duration type...
3 comentarios
Stephen23
el 28 de Jun. de 2023
Editada: Stephen23
el 28 de Jun. de 2023
" It's interesting when I do 0b.VariableTypes on example1, the result is..."
That should not be the result after the STEVARTYPE calls. Please check after the variable types and options have been set. That is rather the whole point of many answer, I would be very surprised if it did not work.
As I wrote in my answer, you can reuse the same options object for all files. It should not change.
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements 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!