0

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 ?

Itban Saeed
  • 1,660
  • 5
  • 25
  • 38
  • Could you give a sample string from a file that you are trying to read (i.e. example how does your input look like)? – selyunin Sep 09 '16 at 07:29

2 Answers2

1

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 = {';',' '};
obchardon
  • 10,614
  • 1
  • 17
  • 33
0

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);
Anders Schou
  • 184
  • 1
  • 13