ragnar
ragnar

Reputation: 57

MATLAB, ASCII files. Textscan? importdata? load? none of them works

I have to import ASCII files in MATLAB and then have them read. These are the functions I tried out with:

1) load(filename) does not work: it says "number of columns on line 2 of ASCII file must be the same as previous lines"

2) textscan(file, '-ascii') returns something like "Empty cell array: 1-by-0

3) importdata(file) returns " data: [2x1 double], textdata: [4x1 cell], colheaders {'*LOS='}. It actually works, as suggested by georgesl, but it treats the full text as a unique column: how can I skip the header and then split the data into 2 columns?

I have noticed everything is all right if I convert the ascii file into a dat one, but I have many files (more than 100) that should be worked out.

What should I do?

Thanks

Upvotes: 4

Views: 24285

Answers (3)

Abbas.Y
Abbas.Y

Reputation: 1

[filename pathname] = uigetfile({'*.txt'}, 'Select File');
fullpathname = strcat (pathname, filename);
    A = importdata(fullpathname,'');
value =getfield(A, 'data');

enjoy it!

Upvotes: 0

PaxRomana99
PaxRomana99

Reputation: 574

I like the approach mentioned by Shai, but generally use the command textscan

data = textscan(fid, '%s', 'Delimiter', '\n')

so that I end up with a cell array of strings. Makes things easier to process if you are worried about line numbers.

Upvotes: 1

Shai
Shai

Reputation: 114786

You can read the entire file into a string using fileread

text = fileread( filename );

Then you can parse it yourself using regexp

Upvotes: 2

Related Questions