Fortran Structures: What Are They?

In summary: C example is helpful in understanding the FORTRAN code. In summary, the FORTRAN code defines a structure called "pixel" that allows you to define specific instances of a pixel with the characteristics of cl, rw, x and y. Somewhere in the code there will be a statement that instantiates a pixel instance, eg something like "myPixel pixel...". "myPixel" is then the name of your pixel instance, and the pixel members for "myPixel" will likely be identified as "myPixel.cl", "myPixel.rw", myPixel.x" and "myPixel.y". For example, "myPixel.x = 100" and "myPixel.y = 200" will establish the
  • #1
cammo12
9
0
Hi - I expect this is quite a simple question.

What are fortran structures? I'm trying to convert fortran code into MATLAB and I don't know what a structure / endstructure statement is.

e.g.


structure /pixel/
union
map
INTEGER*4 cl
INTEGER*4 rw
endmap
map
INTEGER*4 x
INTEGER*4 y
endmap
endunion
endstructure
 
Technology news on Phys.org
  • #2
A structure is a defined block of related variables, a feature common to many languages. I am not sure about Fortran structure specifically, but it appears that you have structure called "pixel" that allows you to define specific instances of a pixel with the characteristics of cl, rw, x and y. Somewhere in the code there will be a statement that instantiates a pixel instance, eg something like "myPixel pixel...". "myPixel" is then the name of your pixel instance, and the pixel members for "myPixel" will likely be identified as "myPixel.cl", "myPixel.rw", myPixel.x" and "myPixel.y". For example, "myPixel.x = 100" and "myPixel.y = 200" will establish the x-y coordinates for the "myPixel" pixel.

Chris
 
Last edited by a moderator:
  • #3
Here is the same thing in C. I have not done MATLAB in so long I'd be afraid to post anything that purports any relationship.
Code:
typedef union
{
      struct
      {
        long cl;
        long rw;
      } s1;
      struct
      {
        long x;
        long y;
      } s2;
} pixel_t;

pixel_t pix;

It's a union of two structs.
 
  • #4
So what is the point in this?

Do I have to have structures? Can't I just use an array somehow?
 
  • #5
Thanks for your help by the way :)
 
  • #6
No you don't have to use structures (and that particular union of two structures seems rather complicated, for what it actually does).

The point of structures is to logically group a set of data items together so you can refer to the group by one name - for instance

pixel p1,p2, pfun() // pfun is a function that returns a pixel structure
...
p1 = p2
call sub(p1)
p2 = pfun(1,2,3)
...
etc

Yes you could do all this without using structures - except for the function return. But if you had a structure with 20 elements not 2, the code would be about 20 times longer.
 
  • #7
Yes you can probably manage with an array, depending on the complexity of the problem. eg, something like:

INTEGER pixel(4)
* then address the members as element of the array
cl = pixel(1)
rw= pixel(2)
x = pixel(3)
y = pixel(4)
 
  • #8
..thought C would help. :)

In English, not computer, the FORTRAN code means the following:
cl occupies the exact same memory location as does x
rw does the same with y
cl, rw, x, and y are all signed integers - 32 bit int -- what INTEGER*4 means.

Your array does not seem to do all that - ie., overlap memory. I'm assuming INTEGER pixel(4) creates four elements in vector of 32 bit signed ints.
 
  • #9
Hi jim

Yes, the array method does not physically overlap in memory the elements cl with x, and rw with y. cammo12, that is what the union does in your original code.
Without structures or unions this can be achieved in FORTRAN by using "equivalence". However, equivalence will not work with members from the same array. So if this type of union is required then two arrays will have to be created:

integer pixelx(2),pixely(2)
equivalence (pixelx(1),pixely(1)), (pixelx(2),pixely(2))
etc, etc

This may not help with the Matlab translation, but hopefully jim's C example and the explanations may help cammo12 to interpret the Fortran snippet.

Chris
 
Last edited by a moderator:

Related to Fortran Structures: What Are They?

1. What is a Fortran structure?

A Fortran structure is a data type that allows the user to group related data together under a single name. It is similar to a record or struct in other programming languages.

2. How is a structure declared in Fortran?

To declare a structure in Fortran, the user must first create a type definition using the "type" keyword, followed by the name of the structure and its components. The structure can then be declared using the "type" keyword and the defined type name.

3. What are the advantages of using structures in Fortran?

Structures allow for more efficient and organized data management in Fortran programs. They can also improve code readability and make it easier to update or modify code in the future.

4. Can structures be nested in Fortran?

Yes, structures can be nested in Fortran. This means that a structure can contain other structures as its components, allowing for more complex data organization.

5. Are there any limitations to using structures in Fortran?

One limitation of using structures in Fortran is that they cannot have variable-length components. This means that the size of each component must be declared at compile time and cannot change during program execution.

Similar threads

  • Programming and Computer Science
2
Replies
62
Views
4K
  • Programming and Computer Science
Replies
2
Views
983
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
4
Views
689
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
2
Replies
37
Views
3K
  • Programming and Computer Science
Replies
20
Views
3K
Back
Top