Got Questions? Get Answers.
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

Thread Subject:
verifying line format

Subject: verifying line format

From: Pedro Pinheiro

Date: 29 Mar, 2011 18:50:04

Message: 1 of 6

Hello.

I'm currently developing a matlab program to read from a txt file which will ne something along the lines of:

%Nodes (not present in the actual txt)
1 2 2
2 3 4
3 4 4

%Elements (not present in the actual txt)
1 2 2 2 2
2 3 4 3 3
3 4 4 4 4
2 3 4 1 1

Basically I'm using textscans with different fomats read the lines either from elements or nodes, which is possible since I have the number of nodes and number of elements (which I also read previously) in the beginning of the file.

What I'm looking for is a way to check if a line is, or not, in a specific format, otherwise if the number of nodes/elements doesn't match the number of nodes/elements in the rest of the file my program will crash. Is there anyway? I didn't manage to find anything about it.

Regards, Pedro Pinheiro

Subject: verifying line format

From: Florin Neacsu

Date: 29 Mar, 2011 19:25:24

Message: 2 of 6

"Pedro Pinheiro" wrote in message <imt9ks$4k2$1@fred.mathworks.com>...
> Hello.
>
> I'm currently developing a matlab program to read from a txt file which will ne something along the lines of:
>
> %Nodes (not present in the actual txt)
> 1 2 2
> 2 3 4
> 3 4 4
>
> %Elements (not present in the actual txt)
> 1 2 2 2 2
> 2 3 4 3 3
> 3 4 4 4 4
> 2 3 4 1 1
>
> Basically I'm using textscans with different fomats read the lines either from elements or nodes, which is possible since I have the number of nodes and number of elements (which I also read previously) in the beginning of the file.
>
> What I'm looking for is a way to check if a line is, or not, in a specific format, otherwise if the number of nodes/elements doesn't match the number of nodes/elements in the rest of the file my program will crash. Is there anyway? I didn't manage to find anything about it.
>
> Regards, Pedro Pinheiro


Hi,

If I'm understanding correctly your problem then you should try and use fgetl

>for i=1:number_of_lines_with_nodes
> temp = fgetl(fid);
> temp = str2num(temp)

and from here look at size(temp) and see if it matches your pattern(in this case 3 numbers).
The same for elements.

Maybe there is a way with textscan, but I do not know how.

Hope it helps.
Florin

Subject: verifying line format

From: Pedro Pinheiro

Date: 29 Mar, 2011 19:51:05

Message: 3 of 6

I've thought about something like what you said, the problem is my program has to read a file of a truss with 7 nodes but it also has to read the file of a truss with 300 nodes, and in that case I'll have nodes like 300 24 24. There fore I can't verify the lines based on their size. The same applies to the elements.

Regards,
Pedro Pinheiro

Subject: verifying line format

From: Florin Neacsu

Date: 29 Mar, 2011 20:25:21

Message: 4 of 6

"Pedro Pinheiro" wrote in message <imtd79$8k2$1@fred.mathworks.com>...
> I've thought about something like what you said, the problem is my program has to read a file of a truss with 7 nodes but it also has to read the file of a truss with 300 nodes, and in that case I'll have nodes like 300 24 24. There fore I can't verify the lines based on their size. The same applies to the elements.
>
> Regards,
> Pedro Pinheiro

Hi,

You mean that the same file contains different number of nodes ? Or you have different types of files with corresponding number of nodes ? Please, give an example of your files.

Regards,
Florin

Subject: verifying line format

From: Pedro Pinheiro

Date: 29 Mar, 2011 21:03:07

Message: 5 of 6

> Hi,
>
> You mean that the same file contains different number of nodes ? Or you have different types of files with corresponding number of nodes ? Please, give an example of your files.
>
> Regards,
> Florin

An example of a file is:

3,2 (number of nodes, number of elements)

1 2 3 %(nodes)(number of the node, x coordinate, y coordinate)
2 3 4
3 4 5

1 1 1
2 2 2
%(elements)(number of the element, node A, node B, material group, geometric group)

I'll have diffrent types of files with corresponding number of nodes.
The purpose of my question is simple, I want to verify the format of the all the lines. What I do at the moment is (summarised):

R = textscan(fid, %f %f', 1, 'delimiter',',;|');
num_nodes = R{1};
num_elements = R{2};
N = textscan(fid, '%f %f %f', num_nodes, 'delimiter',',;|');
E = textscan(fid, '%f %f %f %f %f', num_elements, 'delimiter',',;|');

The problem is if for some reason the number of nodes/elements doesn't match the actual number of nodes/elements, I'll get invalid data or the program will crash. Basically I want to check if a line has a '%f %f %f' format for example.

Subject: verifying line format

From: Florin Neacsu

Date: 29 Mar, 2011 22:07:04

Message: 6 of 6

"Pedro Pinheiro" wrote in message <imtheb$nes$1@fred.mathworks.com>...
> > Hi,
> >
> > You mean that the same file contains different number of nodes ? Or you have different types of files with corresponding number of nodes ? Please, give an example of your files.
> >
> > Regards,
> > Florin
>
> An example of a file is:
>
> 3,2 (number of nodes, number of elements)
>
> 1 2 3 %(nodes)(number of the node, x coordinate, y coordinate)
> 2 3 4
> 3 4 5
>
> 1 1 1
> 2 2 2
> %(elements)(number of the element, node A, node B, material group, geometric group)
>
> I'll have diffrent types of files with corresponding number of nodes.
> The purpose of my question is simple, I want to verify the format of the all the lines. What I do at the moment is (summarised):
>
> R = textscan(fid, %f %f', 1, 'delimiter',',;|');
> num_nodes = R{1};
> num_elements = R{2};
> N = textscan(fid, '%f %f %f', num_nodes, 'delimiter',',;|');
> E = textscan(fid, '%f %f %f %f %f', num_elements, 'delimiter',',;|');
>
> The problem is if for some reason the number of nodes/elements doesn't match the actual number of nodes/elements, I'll get invalid data or the program will crash.



Hi,

"Basically I want to check if a line has a '%f %f %f' format for example."

fgetl is still a good bet in this case.

Is this helpful ?

the text file
-----------------------------
3 2
1 2 3
2 3 4 3 4
3 4 5
1 1 1 2 3
2 2 2 2
2 2 2
----------------------------

matlab code :
---------------------------
fid=fopen('foo.txt','r');

temp=fgetl(fid);
num_nodes = str2num(temp(1));
num_elements = str2num(temp(3));
counter=0;
while 1
temp=fgetl(fid);
if ~ischar(temp), break, end
temp=str2num(temp);
counter=counter+1;

if counter<=num_nodes
    cc=1;
elseif counter<=(num_nodes+num_elements)
    cc=2;
else
    cc=0;
end

switch cc
    case 1
        if size(temp,2)~=3
            fprintf(1,'Warning! expecting 3 arguments\n');
        else
            temp
        end
    case 2
        if size(temp,2)~=5
            fprintf(1,'Warning! expecting 5 arguments\n');
        else
            temp
        end
    otherwise
        fprintf(1,'Unexpected\n'),break;
end
end
fclose(fid);
-----------------------------------------

Regards,
Florin

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us