How to read in a text file with data seperated by a colons and semicolos?

1 view (last 30 days)
Good morning!
I could use some help. I have a .txt file which I have to read a treat but it has a format different to what Matlab understand. It looks like this:
  • 0,40125;-0,0311547518
  • 0,40150;-0,0278959572
  • 0,40175;-0,0236756057
  • 0,40200;-0,0188543797
  • 0,40225;-0,0168816000
and so on. I would like to read the first column and put in a vector and after that, read the second column and put it in another vector. What I would need looks like this:
  • 0.40125,-0.0311547518
  • 0.40150,-0.0278959572
  • 0.40175,-0.0236756057
  • 0.40200,-0.0188543797
  • 0.40225,-0.0168816000
So, here, I would need either a way to convert it to the second sample or just a way to be able to read the data and directly create the arrays.
Any help is greatly appreciated. Thanks, Juan

Answers (1)

Guillaume
Guillaume on 26 Feb 2015
You'll have to use textscan:
fid = fopen('myfile.txt', 'rt');
m = textscan(fid, '%d,%d;%f');
fclose(fid);

Community Treasure Hunt

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

Start Hunting!