Opened 10 years ago

Closed 10 years ago

#2919 closed defect (invalid)

Call model.variable with a parameter

Reported by: hed204 Owned by: adeas31
Priority: high Milestone: 1.9.1
Component: Frontend Version: trunk
Keywords: Cc:

Description

Hi everybody,

I searched a solution for it whithout succes...

there is my problem :

I have in a model two Inertia (Inertia1, Inertia2)

I would like to do this :

for i in 1:2 loop
    M[i]= "Inertia" & i & ".J";
end for;

I look for the result M[1]=Inertia1.J and M[2]=Inertia2.J

Thanks for your help.

Hugo

Change History (5)

comment:1 Changed 10 years ago by adeas31

I don't understand what you want to do. Share complete model.

comment:2 Changed 10 years ago by sjoelund.se

"Inertia" + String(i) + ".J" I guess?

comment:3 Changed 10 years ago by adrpo

I think hed204 wants to simulate several models based on the M[i] name he creates but that doesn't work from normal Modelica you need scripting for that. And I'm not even sure you can do it with scripting, do we have a function that converts from String to TypeName?

comment:4 Changed 10 years ago by hed204

Hi,
Actually I have a model with some Rotationnal.Inertia in. I would like to create a matrix with all inertia (Inertia(number).J). So I did a "for loop"

"Inertia" + String(i) + ".J" works.

But

M[i]="Inertia" + String(i) + ".J"

doesn't work because Real=String

So I am looking for a function to convert a String to a Real (@Adrpo I think that's exactly what you said)

comment:5 Changed 10 years ago by sjoelund.se

  • Component changed from OMEdit to Frontend
  • Resolution set to invalid
  • Status changed from new to closed

Modelica does not support meta-programming. You cannot concatenate strings to create a variable name.

If you want to loop over an array, declare your variables as arrays:

  Intertia intertia[2];
equation
  for i in 1:2 loop
    m[i] = intertia[i].j;
  end for;
Note: See TracTickets for help on using tickets.