-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented reading from Matlab script.m
- Loading branch information
Showing
8 changed files
with
117 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
CaseExamples | ||
ReadRealParameter | ||
ReadReal_m | ||
ReadRealCSV | ||
ReadStringCSV | ||
ReadStringCSVUnquoted | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ class Contact "Contact" | |
<p> | ||
Anton Haumer<br> | ||
<a href=\"http://www.haumer.at\">Technical Consulting & 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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
UsersGuide | ||
Examples | ||
readRealParameter | ||
readReal_m | ||
readRealCSV | ||
readStringCSV | ||
readLineWithoutCache | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |