Skip to content

Commit

Permalink
implemented reading from Matlab script.m
Browse files Browse the repository at this point in the history
  • Loading branch information
AHaumer committed Nov 29, 2021
1 parent 9337e64 commit e43d2f4
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 43 deletions.
22 changes: 22 additions & 0 deletions KeyWordIO/Examples/ReadReal_m.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
within KeyWordIO.Examples;
model ReadReal_m "Reads real parameters from m-file"
extends Modelica.Icons.Example;
import Modelica.Units.SI;
parameter String inputFileName = Modelica.Utilities.Files.loadResource("modelica://KeyWordIO/Resources/m/setParameters.m");
parameter SI.Resistance R0=KeyWordIO.readReal_m(inputFileName, "data.R0");
parameter SI.Resistance R1=KeyWordIO.readReal_m(inputFileName, "data.R1");
parameter SI.Resistance R2=KeyWordIO.readReal_m(inputFileName, "data.R2");
parameter SI.AngularVelocity w0(displayUnit="rpm")=KeyWordIO.readReal_m(inputFileName, "data.w0");
annotation (experiment(StopTime = 1, Interval = 1E-3), Documentation(info="<html>
<p>
To store parameters for usage in Modelica and in Matlab/Simulink only once;
</p>
<ul>
<li>Create a Matlab script which sets the paramaters in a struct in workspace.</li>
<li>For comments, use Matlab.style \"%\".</li>
<li>Terminate the expressions with \";\".</li>
<li>Read this m-file from Modelica.</li>
</ul>
</ul>
</html>"));
end ReadReal_m;
1 change: 1 addition & 0 deletions KeyWordIO/Examples/package.order
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CaseExamples
ReadRealParameter
ReadReal_m
ReadRealCSV
ReadStringCSV
ReadStringCSVUnquoted
Expand Down
5 changes: 5 additions & 0 deletions KeyWordIO/Resources/m/setParameters.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
% script file to set parameters in a Matlab-struct
data.R0 = 2; % 1st parameter
data.R1 = 2.2; % 2nd parameter
data.R2 = 2*3; % 3rd parameter
data.w0 = 1500*2*pi/60; % 4th parameter
2 changes: 1 addition & 1 deletion KeyWordIO/UsersGuide/Contact.mo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Contact "Contact"
<p>
Anton Haumer<br>
<a href=\"http://www.haumer.at\">Technical Consulting &amp; Electrical Engineering</a><br>
3423 St. Andrae-Woerdern, Austria<br>
93049 Regensburg, Germany<br>
email: <a HREF=\"mailto:[email protected]\">[email protected]</a><br>
</p>
Expand Down
8 changes: 7 additions & 1 deletion KeyWordIO/UsersGuide/ReleaseNotes.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ within KeyWordIO.UsersGuide;
class ReleaseNotes "Release Notes"
extends Modelica.Icons.ReleaseNotes;
annotation(Documentation(info="<html>
<h5>Version 0.11.0, 2021-11-29</h5>
<ul>
<li>Read parameters from a Matlab-script</li>
</ul>
<h5>Version 0.10.0, 2021-07-19</h5>
<ul>
<li>Switch to Modelica Standard Library (MSL) 4.0,0</li>
<li>Switch to Modelica Standard Library (MSL) 4.0.0</li>
<li>Fix implementation of C inline code,
see <a href=\"https://github.com/christiankral/KeyWordIO/issues/11\">#11</a></li>
<li>Fix escape <code>\\0</code> warning in OpenModelica,
Expand Down
42 changes: 1 addition & 41 deletions KeyWordIO/package.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,8 @@ within ;
package KeyWordIO "Read and write data files with key words"
extends Modelica.Icons.Package;









































annotation (
version="0.10.X", versionDate = "2021-XX-XX",
version="0.11.X", versionDate = "2021-XX-XX",
uses(Modelica(version="4.0.0")),
conversion(from(version={"0.9.0", "0.8.0", "0.7.0", "0.6.0"},
script="modelica://KeyWordIO/Resources/Scripts/Conversion/ConvertFromKeyWordIO_before_0.9.0.mos")));
Expand Down
1 change: 1 addition & 0 deletions KeyWordIO/package.order
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
UsersGuide
Examples
readRealParameter
readReal_m
readRealCSV
readStringCSV
readLineWithoutCache
Expand Down
79 changes: 79 additions & 0 deletions KeyWordIO/readReal_m.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
within KeyWordIO;
function readReal_m "Read the value of a Real parameter from m-file"
extends Modelica.Icons.Function;
import Modelica.Utilities.*;
input String fileName "Name of file";
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";
protected
Integer iline = 1;
String line;
Integer index;
Integer lenIdent;
String message = "in file \"" + fileName + "\" on line ";
Boolean found = false;
Boolean endOfFile = false;
algorithm
(line, endOfFile) :=KeyWordIO.readLine(fileName, iline, cache);
while not found and not endOfFile loop
if Strings.isEqual(Strings.substring(line,1,2),"//") or
Strings.isEqual(Strings.substring(line,1,1),"%") or
Strings.isEqual(Strings.substring(line,1,1)," ") then
//skip comment lines (even if comment has trailing blanks before "//" or "%")
iline := iline + 1;
else
index := Strings.find(line, "=", 1); //get position of "="
if index <= 0 then //no expression
Streams.error("Erroneous expression without =" + message + String(iline));
else
//strip trailing blanks of identifier part before "="
lenIdent := Strings.find(line," ",1);
lenIdent := if lenIdent <=0 then index else min(index, lenIdent);
if not Strings.isEqual(Strings.substring(line, 1, lenIdent - 1), name) then
//identifier <> name: skip line
iline := iline + 1;
found := false;
else
//identifier == name: evaluate expression
//check for mandatory ";" after formula before optional comments
if Strings.find(line, ";", index+1) <=0 then
Streams.error("Expression not terminated by ;" + message + String(iline));
else
(result, index) := KeyWordIO.Strings.expression(line, index+1, message + String(iline));
found := true;
end if;
end if;
end if;
end if;
(line, endOfFile) :=KeyWordIO.readLine(fileName, iline, cache);
end while;
if not found then
Streams.error("Parameter \"" + name + "\" not found in file \"" + fileName + "\"");
end if;
annotation(Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
result = <b>readReal_m</b>(fileName, name);
</pre></blockquote>
<h4>Description</h4>
<p>
This function demonstrates how a function can be implemented
that reads the value of a parameter from a Matlab script-file.
The idea is to keep parameters in Modelica and in Matlab/Simulink up to date.
The function performs the following actions:
</p>
<ol>
<li> It opens file \"fileName\" and reads the lines of the file.</li>
<li> If a line starts with \"% \" or \"// \" or \" \", it is skipped.</li>
<li> If a line starts with \"name = expression\" and the \"name\"
in this line is identical to the second argument \"name\"
of the function call, the expression calculator
is used to evaluate the expression after the \"=\" character.
The expression has to be terminated with a \";\".
After the \";\" a comment can follow.</li>
<li> The result of the expression evaluation is returned as
the value of the parameter \"name\". </li>
</ol>
</html>"));
end readReal_m;

0 comments on commit e43d2f4

Please sign in to comment.