result = readRealParameter(fileName, name);


This function demonstrates how a function can be implemented that reads the value of a parameter from file. The function performs the following actions:

  1. It opens file "fileName" and reads the lines of the file.
  2. In every line, Modelica line comments ("// ... end-of-line") are skipped
  3. If a line consists of "name = expression" and the "name" in this line is identical to the second argument "name" of the function call, the expression calculator Examples.expression is used to evaluate the expression after the "=" character. The expression can optionally be terminated with a ";".
  4. The result of the expression evaluation is returned as the value of the parameter "name".


On file "test.txt" the following lines might be present:

// Motor data
J        = 2.3     // inertia
w_rel0   = 1.5*2;  // relative angular velocity
phi_rel0 = pi/3

The function returns the value "3.0" when called as:

readRealParameter("test.txt", "w_rel0")


function readRealParameter
  extends Modelica.Icons.Function;
  import Modelica.Utilities.*;
  import KeyWordIO;
  input String fileName "Name of file" annotation(
    Dialog(saveSelector(filter = "Data file (*.txt;*.dat)", caption = "Open text file for reading")));
  input String name "Name of parameter";
  input Boolean cache = false "Read file before compiling, if true";
  output Real result "Actual value of parameter on file";
end readRealParameter;

Generated at 2021-03-06T02:01:02Z by OpenModelicaOpenModelica 1.18.0~dev-127-g8b9b4e0 using GenerateDoc.mos