Itban Saeed
Itban Saeed

Reputation: 1666

MATLAB: Reading space separated float values from tex file

I am reading a text file using the textscan function of MATLAB. Problem here is that nothing is being read in value as the floating points are separated with three spaces and I am quite new in MATLAB programming to use some efficient syntax. My current code is given below:

Code:

values = textscan(input_file, '%f   %f   %f   %f   %f\n %*[^\n]');

The input file follows the following format:

File:

0.781844   952.962130   2251.430836   3412.734125   4456.016362
0.788094   983.834855   2228.432996   3196.415590   4378.885466
0.794344   967.653718   2200.798973   3119.844502   4374.097695

If the floating point values are # separated then the below statement works fine:

values = textscan(input_file, '%f#%f#%f#%f#%f\n %*[^\n]');

Is there any solution except for tokenization ?

Upvotes: 0

Views: 354

Answers (2)

obchardon
obchardon

Reputation: 10792

You need to specify a delimiter, also you should activate the MultipleDelimsAsOne in order to treat the repeated space as a single delimiter:

value = textscan(input_file, '%f   %f   %f   %f   %f \n ','Delimiter',' ','MultipleDelimsAsOne',1);

If needed you can also specify several delimiters at the same time:

del = {';',' '};

Upvotes: 1

Anders Schou
Anders Schou

Reputation: 184

If you don't have to use textscan, you could probably use importdata. There you can specify the delimiter as a parameter. Documentation http://se.mathworks.com/help/matlab/ref/importdata.html

Code example

filename = 'myfile01.txt';
delimiterIn = '   ';
A = importdata(filename,delimiterIn);

Upvotes: 0

Related Questions