Array of structs can be seen in the outer loop only

Hello guys,

I have an array of structs and I had no problem with it until I made a double loop
my array of structs is not defined inside the inner loop whereas in the outer loop, its is fine
How can this be explained? why does this happen…
look here

Please post code instead of a screenshot.

2 Likes
using CSV
df=CSV.read("F:/B/Mayar/lineData.CSV")

mutable struct Linestruct
    buses::Vector{Int}
    res::Float64
    ind::Float64
    imp_mag::Float64
    imp_angle::Float64
    y::Complex{Float64}
    p::Float64
    q::Float64
    state::String
end
CREATE_Linestruct() = Linestruct([0,0],0.0,0.0,0.0,0.0,0.0,0.0,0.0,"unknown")
#Linestruct(buses_line, res_line, ind_line) = Linestruct(buses_line, res_line,
    #ind_line, 0.0, 0.0, 0,0, 0.0, "unknown")
#l2 = Linestruct([1,2,3], 0.0, 0.0)

l3=CREATE_Linestruct()
number_lines=size(df,1)
x=1;
array_lines=Array{Linestruct,1}()

for x in 1:number_lines
temp_struct=CREATE_Linestruct()
push!(array_lines,temp_struct)
x=10
end

f=0
for x in 1:number_lines
#*#*#*#*#*#*#*#*# physical values #*#*#*#*#*#*#*#
array_lines[x].res=df[x,3] # line resistance
array_lines[x].ind=df[x,4] # line inductance
#*#*#*#*#*#*#*#*# impedance calculation - magnitude #*#*#*#*#*#*#*#
temp_mag=(array_lines[x].res)^2+(array_lines[x].ind)^2
array_lines[x].imp_mag=round(sqrt(temp_mag),digits=5)
#*#*#*#*#*#*#*#*# impedance calculation - angle  #*#*#*#*#*#*#*#
temp_ang=atan(array_lines[x].ind,array_lines[x].res)
array_lines[x].imp_angle=round(temp_ang,digits=5)
#*#*#*#*#*#*#*#*# line admittance #*#*#*#*#*#*#*#
# begin by calculating y magnitude as the reciprocal of impedance
temp_y_mag=1/array_lines[x].imp_mag
# then calculate the angle of line admittance by putting a negative
temp_y_angle=-1*array_lines[x].imp_angle
# x= rcostheta # y=rsintheta
temp_x=round(temp_y_mag*cos(temp_y_angle),digits=5)
temp_y=round(temp_y_mag*sin(temp_y_angle),digits=5)
array_lines[x].y=temp_x+(temp_y)im
#*#*#*#*#*#*#*#*# Joining busses #*#*#*#*#*#*#*#
array_lines[x].buses=[df[x,1],df[x,2]] # buses
end
#*#*#*#*#*#*#*#*#*#*#* generating the ybus *#*#*#*#*#*#*#*#
x=1;
xx=1;
number_buses=3  # to try only
y_bus=zeros(Complex{Float64},number_buses,number_buses)
for x in 1:number_buses
#outer loop to create Y11, Y22, Y33 ....YNN
temp_accumulator=0+0im
      for xx in 1:number_lines
#inner loop
            if (array_lines[xx].buses[1]===x) || array_lines.buses[2]===x
                  temp_accumulator=array_lines[xx].y
                  y_bus[x,x]=temp_accumulator+array_lines[xx].y

            end

      end

end

The next step is to make it reproducible without that file. Someone should be able to copy paste it into the REPL and have the error reproduce.

1 Like

sorry, I do not get that

We don’t have that .csv flie on your computer.

You should construct a DataFrame that looks like the file on your computer using your script. Something like

df = DataFrame(a = rand(10), b = rand(10))
2 Likes

aha Okay, I will

I need to create a data frame with size of 3 x 6 .

df = DataFrame(A = 1:3, B = 4:6,C=7:9,D=10:12,E=13:15,F=16:18])

I agree with the above replies that it’s a lot easier to help when you post a self-contained piece of code that reproduces the error you’re seeing. However, looking at the code in your second post and then at the error message in your first post, I think I see what’s happening. You have a variable array_lines, which is a vector with elements of type Linestruct. Linestruct has a field called buses. The error in your first post is not saying that your array is not defined. It is saying that the type Array has no field called buses. This occurs because in your inner loop you have the statement

array_lines.buses[2]===x

which I think is a typo. It should probably be

array_lines[xx].buses[2]===x

array_lines is just an array. The array itself has no field called buses.

1 Like