How to skip first three lines in Matlab and read the next line until fixed character appears?

34 views (last 30 days)
I have a input txt file as shown below:
*Heading
U:/Mesh_Sphere_faible.inp
*Node
1, 0.061725, 0.271605, 0.41523577
2, 0.05795391, 0.39902727, 0.29566634
3, 0.012345, 0.054321, 0.083047155
*Element, type=C3D10, ELSET=PART1
1, 150, 1278, 1280, 1282, 3738, 3742, 3740,
3739, 3741, 3743
2, 62, 700, 702, 704, 2342, 3747, 3745,
3744, 3746, 3748
3, 242, 1863, 1866, 1865, 3749, 3753, 3751,
3750, 3752, 3754
I want to read the data after line of '*Node'. I have tried
A=dlmread('Mesh_Sphere_faible.dat','',4,0)
but always stopped at line:
2, 0.05795391, 0.39902727, 0.29566634
How can I realise it?
And I also want to read until line
*Element, type=C3D10, ELSET=PART1
appears.
What can I do?
  2 Comments
Walter Roberson
Walter Roberson on 28 Mar 2019
Edited: Walter Roberson on 28 Mar 2019
Could you confirm that the lines alternate between 8 comma separated values (with trailing comma on the line) followed by 3 comma separated values?
Or is that just a result of how it was posted, and all of those values are on the same line?
Can you attach a sample file for certainty ?
KOU DU
KOU DU on 28 Mar 2019
Yes, of course. It's clearer if I attach a sample file.
When you see data after *Element like
1, 150, 1278, 1280, 1282, 3738, 3742, 3740,
3739, 3741, 3743
It means they are in the same element but not show in the same line.
I need read them in the same line by Matlab.
Thank you for your answer.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 29 Mar 2019
This will do it:
% Open the file.
fullFileName = fullfile(pwd, 'example.txt')
fileID = fopen(fullFileName, 'rt');
% Read the first 3 lines of the file.
textLine = fgetl(fileID); % Read and throw away line 1
textLine = fgetl(fileID); % Read and throw away line 2
textLine = fgetl(fileID); % Read and throw away line 3
data = [];
while ischar(textLine)
% Read the next line.
textLine = fgetl(fileID);
% Print out what line we're operating on.
fprintf('%s\n', textLine);
if ~contains(textLine, 'Element')
numbers = sscanf(textLine, '%f,')
data = [data; numbers'];
else
break;
end
end
% All done reading all lines, so close the file.
fclose(fileID);
% Show in command window.
data

More Answers (2)

Niti K
Niti K on 27 Mar 2019
you can use the function fgetl. you can open the file in read mode using fopen and set up a while loop with the file id.
sort of a pseudocode
1) open the file using fopen
2) utilizing a loop, use getl which will fetch all the characters in the current line. Each usasge of getl will fetch the next line in your file
3) string split the output from getl using space delimeter
4) use string compare to check whether first worf from string split matches your criteria (*node in this case)
5) you can then read the data you want untill you encounter (*element)
exit out of the loop

Akira Agata
Akira Agata on 29 Mar 2019
Another possible solution:
% Read the file as a text
fid = fopen('Example.txt','r');
C = textscan(fid,'%s','Delimiter','\n');
C = C{1};
fclose(fid);
% Find row numbers which contains the words 'Node' and 'Element'
row1 = find(contains(C,'Node'));
row2 = find(contains(C,'Element'));
% Extract data rows
C = C(row1+1:row2-1);
% Convert to numeric matrix
D = cellfun(@(x) str2double(strsplit(x,',')),C,'UniformOutput',false);
D = cell2mat(D);
  2 Comments
KOU DU
KOU DU on 7 Oct 2019
Hello, Akira
I try to use your method to read the lines after 'Element'. I change C = C(row1+1:row2-1); to C = C(row2+1:end); but it does not work.
It may be the problem that strsplit(x,',') can't split data by ','.
Can you give my some advice? Thanks

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!