MATLAB Answers

Sandeep
0

Read .dat file in matlab

Asked by Sandeep
on 3 Feb 2013

I have a .dat mesh file which looks something like this

v 0 1 2 3

v 2 4 5

..

Any idea how I can read the file in matlab and find the number of occurrences of a particular string and also the number of integers in a particular row?

  1 Comment

Suppose, I want to extract a certain data by arranging the entire data in the file in an array provided each row has different number of data like

v 1 2 3 4 6

v 1 2 0

v 3 4 9 2..

Also, is there any way to find the number of numbers in each row?

Products

No products are associated with this question.

1 Answer

Answer by Image Analyst
on 3 Feb 2013
Edited by Image Analyst
on 3 Feb 2013

Just read it in line by line with fgetl(). Then use strfind() on each line to see if some sequence of numbers you're interested in exists in the string.

fid = fopen('sandeep data.dat);
tline = fgetl(fid);
while ischar(tline)
    disp(tline)
    tline = fgetl(fid);
    if strfind(s, '2 4 5') > 0
      message = 'Found it';
      uiwait(msgbox(message));
end

  2 Comments

Thanks.. I have another question.. Suppose, I want to extract a certain data by arranging the entire data in the file in an array provided each row has different number of data like

v 1 2 3 4 6

v 1 2 0

v 3 4 9 2..

Also, is there any way to find the number of numbers in each row?

Just count the spaces. The number of spaces equals the number of numbers:

numberOfNumbers = sum(s == ' ')

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!