I am trying to translate matlab/gnu octave code into gfortran language code.
I have done declaration of struc to form an array as it is written in matlab. However the compiler will not process the SIZE command due to that array issue that I do not understand. Below is the octave code I am trying to translate
function test1
clc;
close all;
start= 5;
function out = function1(start)
myarray = struct(...
'list1', {'A', 'Cc', 'B', 'E', 'F', 'G', 'H', 'I'}, ...
'list2', {1, 2, 3, 4, 5, 6, 7, 8},...
'list3', {3, 3, 3, 3, 3, 2, 2, 2});
function list1 = function2(list2)
done = false;
myarray
numel(myarray)
ra = randi([1 numel(myarray)], 1, 1)
myarray(ra).list2;
list1 = myarray(ra);
if list1.list1 == 'E'
list1.list1 = round(randn(1,1) * 5);
end
end
x = {};
y = [];
list2 = 0;
list1 = function2(list2);
list2 = list2 + list1.list3;
out = struct('x', {x}, 'y', {y});
end
function1(5)
end
The output of the octave code is,
test1
myarray =
1x8 struct array containing the fields:
list1
list2
list3
ans = 8
ra = 1
ans =
scalar structure containing the fields:
x = {}(0x0)
y = [](0x0)
My gfortran code is,
function function1(start) result(out1)
type myarray
character, dimension(8)::list1=(/'A','C','B','E','F','G','H','I'/);
integer, dimension(8)::list2 =(/1,2,3,4,5,6,7,8/);
integer, dimension(8)::list3 =(/3,3,3,3,3,2,2/);
end type
integer :: start;
integer :: out1;
integer :: sd;
contains
function function2(list2) result(list1)
integer, intent(in) :: list2; ! input
integer :: list1; ! output
logical :: done;
integer, dimension(1,1) :: ra;
real :: rnd1;
done = .FALSE.
call random_number(rnd1);
ra = nint(rnd1*5);
sd= size(myarray);
! ra = nint(rnd1*size(myarray));
print*, "random number is ", ra;
myarray(ra)
end function function2
end function function1
program test1
use iso_fortran_env
implicit none
integer :: start, xout;
real :: ra;
integer :: function1;
start =5;
xout=function1(5);
end program test1
The error message I get from build command is:
gfortran -Ofast -Wall -o "test1" "test1.f90"
test1.f90:32:16:
myarray(ra).list2;
1
Error: Derived type ‘myarray’ cannot be used as a variable at (1)
test1.f90:29:17:
sd= size(myarray);
1
Error: ‘array’ argument of ‘size’ intrinsic at (1) must be an array
Compilation failed.
I believe the issue is my declaration of structure of the array. I am missing something there. Any suggestions? Note that in gfortran code in list1 values I had to change character 'Cc' into 'C' to make it work.