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

New to MATLAB?

How can i import numerical data from non-tabular text file in MAtLAB?

Asked by siddhesh rane

siddhesh rane (view profile)

on 20 May 2013
solid rodfin
   facet normal 9.997519e-001 2.227527e-002 0.000000e+000
      outer loop
         vertex 6.477990e+001 3.459198e+001 1.220000e+002
         vertex 6.485231e+001 3.242616e+001 1.220000e+002
         vertex 6.485231e+001 3.242616e+001 2.775558e-014
      endloop
   endfacet

I have text file as shown above..I only want to import numerical data from it.

2 Comments

Walter Roberson

Walter Roberson (view profile)

on 20 May 2013

Do you have the same number of "vertex" entries for each "facet" entry? So could each "facet" be converted to a 4 x 3 numeric array?

siddhesh rane

siddhesh rane (view profile)

on 20 May 2013

yes i have same number of vertex for each facet..yes it can be converted. how can i do that?

siddhesh rane

siddhesh rane (view profile)

Products

No products are associated with this question.

2 Answers

Answer by David Sanchez

David Sanchez (view profile)

on 20 May 2013
Accepted answer

If test.txt is your txt file, the following code will create a cell C whose cell contains your data ( all of it ). You can iterate along the cell and retrieve the data you need/want.

fid=fopen('test.txt');
C=textscan(fid,'%s');

1 Comment

siddhesh rane

siddhesh rane (view profile)

on 20 May 2013

Thank you very much..that was helpful.

David Sanchez

David Sanchez (view profile)

Answer by David Sanchez

David Sanchez (view profile)

on 20 May 2013
fid=fopen('test.txt');
C=textscan(fid,'%s%s%s%s%s%s%s%s','delimiter','\n'); % read each line in string format
fclose(fid)
data1 = char(C{1,2});
data1 = textscan(data1,'%s%s%f%f%f');
data1 = data1(3:5);
for n = 1:3
    num_data1(n) = data1{n};
end
num_data = zeros(3,3);
for k =4:6
    data= char(C{1,k});
    data = textscan(data,'%s%f%f%f%f');
    data = data(2:4);
    for n = 1:3
        num_data(k-3,n) = data{n};
    end
end

0 Comments

David Sanchez

David Sanchez (view profile)

Contact us