Filter löschen
Filter löschen

How to import data from a non-symmetric .txt file.

3 Ansichten (letzte 30 Tage)
Jeppe Sørensen
Jeppe Sørensen am 19 Sep. 2017
Bearbeitet: Cedric am 21 Sep. 2017
We have the following data file:
1
2 3
5 6 7
How can I import that into MATLAB easily, so the empty block become zero? Like this:
1 0 0
2 3 0
4 5 6
load does not work and importdata gives:
1 NaN
2 3
4 5
6 NaN

Akzeptierte Antwort

Cam Salzberger
Cam Salzberger am 19 Sep. 2017
Hello Jeppe,
If you give an explicit format to use, the data read functions will try to stick to it as best as possible. My personal preferred data read function is readtable. Just tell it not to assume there are header lines (which is probably why it's skipping the "1" by default), and read using your desired format:
t = readtable('datafile.txt','ReadVariableNames',false,'HeaderLines',0,'Format','%f %f %f');
-Cam
  5 Kommentare
Walter Roberson
Walter Roberson am 20 Sep. 2017
readtable() delegates to textscan() which does not need spaces between the % format elements.
Jeppe Sørensen
Jeppe Sørensen am 21 Sep. 2017
By converting NaN to zeros after importing, this works excellently for me. Thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Cedric
Cedric am 20 Sep. 2017
Bearbeitet: Cedric am 20 Sep. 2017
If you are stuck with usual tools, try this:
content = fileread( 'virusDat.txt' ) ;
% - Split in rows, remove extra empty ones.
data = strsplit( content, '\n' ) ;
while isempty( data{end} )
data(end) = [] ;
end
% - Convert to numeric and get max number of columns.
data = cellfun( @(x) sscanf( x, '%d' ), data, 'UniformOutput', false ) ;
nCols = max( cellfun( @numel, data )) ;
% - Define padding function and pad with e.g. NaNs.
pad_fun = @(x) [reshape( x, 1, [] ), repelem( NaN, 1, nCols-numel( x ))] ;
data = cellfun( pad_fun, data, 'UniformOutput', false ) ;
% - Concatenate padded rows.
data = vertcat( data{:} ) ;
EDIT : replaced
nCols = numel( data{end} ) ;
with
nCols = max( cellfun( @numel, data )) ;
in case you don't always have this pyramidal structure.
  2 Kommentare
Jeppe Sørensen
Jeppe Sørensen am 21 Sep. 2017
This works as intended as well, and is a very good answer. Thank you for this. Cam Salzburgs answer gets accepted, however, as it is a bit simpler.
Cedric
Cedric am 21 Sep. 2017
Bearbeitet: Cedric am 21 Sep. 2017
My pleasure! It was just to give you an alternate approach for dealing with "unorthodox" files. Note that it doesn't require that you hard-code 24 for defining a format string and that it is faster than READTABLE, essentially because of the detection of options in READTABLE for importing that takes time.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Standard File Formats finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by