How to create text files (.txt) from Dymola

Text files, with the file extension .txt, are a popular choice for storing data. Lightweight and small in size, they are easy to read, update and distribute. By virtue of storing only the data regarding the ASCII characters within, often only a simple programme such as notepad is required to view, and edit the data within.

Compared to a .mat file, they are a more flexible solution to data storage. Dymola can read text files as easily as .mat files, using the Modelica stream utility readFile. Only the path is required by this function, with the contents of the text file output within Dymola as a stringVector. But how do we write a text file from Dymola?

‘The text file code can be accessed, to copy and paste, here‘.

The Modelica.Utilities.Streams.print command

The simplest and easiest way to create, or add data to a text file in Dymola is to call the Modelica.Utilities.Streams.print function. When supplied with a filename as the second argument, the file is opened and the first argument (string data) is appended to the end of the file (on a new line). However, if the file does not exist in the working directory, it is created with the first argument data inside. Text files can thus be built up line by line this way although the file would be opened for each line written.

The string “Simulation result was:” will either be appended to the file testData.txt , or the first line in a newly created file of the same name depending on whether the file is already present in the working directory.

Treat the commands window like a scripting window

Earlier in the year my colleague explained how to preserve the translation log from a simulation. With a little adaptation, the same method can be used to write bespoke text file from Dymola. Here’s how:

Found by default at the bottom of the simulation tab window in Dymola, the Commands window acts in much the same way as a command prompt in Windows. The command clearlog() removes all previous entries from the Commands window, meaning it is now blank.

The string is now printed in the Commands window before being saved to the text file.
The string is now printed in the Commands window before being saved to the text file.

During simulation of a function, the command Modelica.Utilities.Streams.print actually displays the argument into the Commands window at the time of the call if there is no file path supplied, not the simulation log at the end of simulation as would a model. This means text files can be built up line by line in the command window, before being saved all in one go using the savelog() command and the desired filename. An output of “= true” can be suppressed from the final file by making the savelog() command equal a Boolean variable in the function.

Building up the file

As the print command works line by line, all the data is required to be compiled into a single string before being fed to the print command. Modelica supports appending Strings together using the ” + ” operator.

Here the example test data is added to some other strings providing more information. These strings could be other data.
Here the example test data is added to some other strings providing more information. These strings could be other data.

With large strings, this makes for tricky and confusing coding. Thankfully, within the Claytex library there is an alternative, the Claytex.Functions.Strings.join function. Particularly useful when compiling rows of data, this function is supplied a vector of strings which are delineated with the second argument, which is also a string. This means rows of data separated by commas for instance can be joined easily, ready to be written into a file.

Real, Boolean and Integer data can be converted into String parameters easily, using the String() call. Precision can be specified by setting the significantDigits argument to an integer specifying the desired significant digits. Alternatively, if full data precision of real values is to be maintained, then the call realString() can be used instead.

Encoding formats

Text files can be encoded with using the UTF8 format easily, by setting the following flag:

Advanced.AlwaysWriteUTF8 := true;

Either in the Command window or within a function. It must be remebered that Dymola currently will only support encoding in the UTF8 BOM standard as of Dymola 2020.

Closing remarks

Creating text files from Dymola is a useful solution to handling and saving data whilst not having to deal with trajectory or .mat files. A useful and effective option when needing to distribute data, without needing to worry about proprietary software to read the results file.

Written by: Theodor Ensbury – Project Engineer

Please get in touch if you have any questions or have got a topic in mind that you would like us to write about. You can submit your questions / topics via: Tech Blog Questions / Topic Suggestion


Got a question? Just fill in this form and send it to us and we'll get back to you shortly.


© Copyright 2010-2024 Claytex Services Ltd All Rights Reserved

Log in with your credentials

Forgot your details?