thDvrs
thDvrs

Reputation: 33

How to omit parts of a line while reading a text file in MATLAB (2016 and above)

I need to develop a MATLAB code to read a text file. The lines have the following form:

|     1 |   1   |      6.000 |    454.000 |      423 |

|     1 |   1   |     11.000 |   -454.000 |      426 |

|     1 |   1   |     45.000 |    454.000 |      428 |

Is there a way to omit the vertical bars (and just keep the numbers?)

Thanks a lot!

Upvotes: 0

Views: 38

Answers (1)

sco1
sco1

Reputation: 12214

MATLAB offers multiple different file IO options

For example, you could use textscan:

fID = fopen('test.txt');
test = textscan(fID, '%*u %u %u %f %f %u', 'Delimiter', '|');
fclose(fID);

Which returns an n x 5 cell array of your columns that you can manipulate/concatenate as desired.

Or you could use readtable:

mydata = readtable('test.txt', 'Delimiter', '|', 'Format', '%*u %u %u %f %f %u');

Which returns an n x 5 table of your data.

Upvotes: 1

Related Questions