Skip to content Skip to sidebar Skip to footer

How to Read Mat Files in R

Written by: Garron Fish – Chief Engineer

This blog post shows how to assign Dymola model parameters to elements of a structure stored in a .mat file.

In Modelica 3.2.2 a new office is introduced chosen Modelica.Utilities.Stream.readRealMatrix() which allows users to read a Matlab .mat file saved in the v4, v6 or v7 formats (run into Figure i). Version vii formatting is used in this blog mail as this format supports the reading of information structures in Dymola 2017.

figure-1-the-information-layer-of-the-modelia-utilities-stream-readrealmatrixfunction

Figure 1: The information layer of the Modelia.Utilities.Stream.ReadRealMatrix() function.

Notation that, on one paw, the readRealMatrix function in Figure 1 tin can merely read real matrices with 2 dimensions, simply on the other mitt, both Dymola 2022 and 2022 FD01 do not support the v7.3 format.

For the purpose of illustrating how to assign a Dymola model parameters to elements of a data structure stored in a .mat file, first we demand to create a data structure and store in a .mat file either using Octave or Matlab® every bit in Effigy two.

figure-2-octave-commands-to-create-a-simple-data-structure-and-save-it-with-matlab-v7-format

Figure 2: Octave commands to create a simple information structure and save it with matlab-v7 format.

An example of extracting the matrix created above from the stored .mat file using the readRealMatrix function is shown in Figure 3.

figure-3-an-example-of-using-the-readrealmatrix-function

Figure iii: An example of using the readRealMatrix function.

In Figure iii, the gearMap assortment in the vehicle structure is extracted by referring to this element equally "vehicle.transmission.gearMap"

By executing the above role, yous volition come across the following result in command log pane of the Dymola command window.

figure

Alternatively, this function can also be used in Modelica classes. Two such examples are illustrated below : (one) with a known matrix dimensions and (2) with an arbitrary matrix dimensions.

(1) An instance with a known matrix dimensions

          model ExtractVehicleData
          parameterReal rightShocks_c =scalar(Modelica.Utilities.Streams.readRealMatrix(            "Vehicle.mat", "vehicle.chassis.frontSuspension.rightShocks.c",1,i));
parameterReal rightShocks_d =scalar(Modelica.Utilities.Streams.readRealMatrix(            "Vehicle.mat", "vehicle.chassis.frontSuspension.rightShocks.d", 1, one));
parameterReal gearMap[four,6] =Modelica.Utilities.Streams.readRealMatrix(           "Vehicle.mat", "vehicle.transmission.gearMap", iv, 6);
// code omitted//
          end ExtractVehicleData;

(two) An example with an capricious matrix dimensions

The Modelica.Utilities.Stream.readMatrixSize() function can be used to determine the size of the matrix being read in.  So, another way of reading the gearMap parameter that does not require prior cognition of the size of the gearMap is as follows:

parameterReal gearMap[ Modelica.Utilities.Streams.readMatrixSize("Vehicle.mat","vehicle.manual.gearMap")*{1,0}, Modelica.Utilities.Streams.readMatrixSize("Vehicle.mat","vehicle.transmission.gearMap")*{0,1}] =Modelica.Utilities.Streams.readRealMatrix("Vehicle.mat", "vehicle.transmission.gearMap", Modelica.Utilities.Streams.readMatrixSize("Vehicle.mat","vehicle.transmission.gearMap")*{1,0}, Modelica.Utilities.Streams.readMatrixSize("Vehicle.mat","vehicle.manual.gearMap")*{0,i});

Please go far touch if you lot have whatever questions or accept got a topic in listen that yous would like us to write about. Yous tin can submit your questions / topics via: Tech Blog Questions / Topic Proposition .

taylorulsiny.blogspot.com

Source: https://www.claytex.com/blog/reading-structures-from-mat-files/

إرسال تعليق for "How to Read Mat Files in R"