On the Peculiarities of the Exchange of Data between Applications in High-Level Languages and Matlab Functions Alexander V. Krasnovidov Anatoly D. Khomonenko Department of Information and Computer Department of Information and Computer Systems, Emperor Alexander I St. Petersburg Systems, Emperor Alexander I St. Petersburg State Transport University State Transport University St. Petersburg, Russia St. Petersburg, Russia alexkrasnovidow@mail.ru khomon@mail.ru Andrew V. Zabrodin Alexander V. Smirnov Department of Information and Computer Laboratory of computer aided integrated Systems, Emperor Alexander I St. Petersburg systems, St.Petersburg Institute for State Transport University Informatics and Automation of the RAS St. Petersburg, Russia Saint Petersburg, Russia zaw2003@yandex.ru smir@iias.spb.su Currently, a large number of different data processing systems are applied, among which one of the most frequently used is the Matlab system. However, this system is practically no Abstract means of interactive interaction with users. In The possibilities of data exchange between addition, a program written in the Matlab language functions in the language of the Matlab can be run to perform only inside the own system with applications in high-level environment. On the other hand, programs languages are considered. Programs in developed in high-level C ++ or C # languages provide users with advanced interactive high-level languages appropriately to use in possibilities, but are lack internal means for conjunction with mathematical packages to complex mathematical or statistical data processing. collect data, control various technological Hence the expediency of combining the processes, and perform other similar computational capabilities of specialized data actions. The instrumental means of processing systems with the interactive capabilities organizing such an exchange are of programs in high-level languages. The characterized. It had been shown the integration technology of MATLAB functionality feasibility of integrating applications in allows Windows users to maximize the use of the high-level languages and the M-function of analytical tools of this software package for the the Matlab system, if necessary, to organize following tasks: input / output of large amounts of data. ● solving optimization problems of any dimension; Introduction ● obtaining reliable results of financial and economic calculations, including on-line; A modern approach to the design of information ● three-dimensional visualization of complicated geometric shapes and surfaces having systems requires maximum information an economic meaning; compatibility of various software applications that ● creating business valuation algorithms that work in a single information environment.1 require a large amount of computation; ● creation of intelligent project management Copyright © by the papers’ authors. Copying systems; permitted for private and academic purposes. ● modeling of economic processes of any In: B. V. Sokolov, A. D. Khomonenko, A. A. degree of complexity using the capabilities of Bliudov (eds.): Selected Papers of the Workshop the Simulink package. Computer Science and Engineering in the framework of the 5 th International Scientific- Education", St.-Petersburg, Russia, 8–9 November, Methodical Conference "Problems of Mathematical 2018, published at http://ceur-ws.org and Natural-Scientific Training in Engineering 33 The main methods of organizing such an mentioned property does not allow their interaction are described in detail in various papers, integration into the above dynamic libraries. Hence for example, in [Ada17, Smo05, Kra18] and others. the need to find other possibilities for data These are the following methods: exchange between interrelated M-functions within a 1. Program conversion, written in Matlab single task is following. One of the possible way of language, into program in C ++. This method is solving is the organization of data exchange historically the first. between interconnected M-functions through the 2. Matlab system interaction with Microsoft calling program in C ++ or C #. The above way is Visual Studio using the NET Framework platform. considered in this article. Each of these methods has its own advantages and disadvantages. A detailed comparison of them is given in [Ada17, Bey05]. Here it is worth noting 1 Organization of the Data Exchange that independent applications use a special mode of between Matlab-Functions and operation of the mcc compiler, which leads to Programs in High-Level Language difficulties in passing parameters to the Matlab function. In addition, such an application is It was shown above that one of the options for launched from the command line, which restricts organizing the exchange of data between the dialog capabilities. In fact, the mcc compiler interrelated M-functions is calling a method written generates C ++ code that is equivalent to calling the in one of the high-level languages (C ++ or C #). In corresponding Matlab function [Mat09]. this case, the common data is located in the address The second method is based on the creation of space of the calling program and transmitted to the dynamic class libraries according to CLR standards called M-functions as parameters. The general that can be used in any application, which scheme of organizing such kind of interaction is appropriates these standards. Versions of the described in detail in [Ada17]. The following Matlab system, starting with R2010b and higher, peculiarities of data transmission between M- allow creating dynamic libraries which can be functions and a high-level language program are used in programs in any programming languages considered below. supported by the CLR runtime. 1. Programming languages C and C # are In this case, the M-functions are converted to languages with strong data typing. All variables methods of objects whose classes are declared in must be declared before they are used. Matlab the CLR. Further these methods can be called from programming system does not require preleminary programs in languages C ++ or C #. This approach declaration of variables and their types. allows you to fully utilize the capabilities of the 2. Methods in C ++ and C # applications have Microsoft system Visual Studio in terms of or one output parameter returned by the return supporting interactive tools and flexibility in statement and its type coinciding the method type, managing the computational process. However, in or without output parameter (void type). M - its implementation should be kept in mind the functions can return multiple output variables. In following features of the classes declared: general case, the header of the M function has the ● M-functions, unlike C ++ or C # methods, can following form: have several output variables. ● Declared classes can't encapsulate data. They function [y1, y2,…,yn] = < function_name > can contain only callable methods. (). ● Script-files can't be converted to methods and integrated into a dynamic library. Thus, the output variables are a vector with The first of the properties listed is not elements y1, y2, ..., yn, while all of them, in turn, significant in cases when a call from a C ++ / C # can be matrices or vectors. Each element can relate program applies to a method that uses only data to different data types. During the execution of the transmitted from the calling program, and returns M - function, all output variables should be the result only to it (standalone method). The other assigned values. An example of such a function is two properties can lead to difficulties in a various the meshgrid function, which is used in conjunction situations, for example, in case of necessity of data with graphing functions for three-dimensional exchange between methods contained in the surfaces. The call to this function is: dynamic library. An example of this is a Matlab program that consists of several interrelated M- [X,Y] = meshgrid(x), where X and Y – output functions, some of which perform initialization of vectors (the result of the function). common variables, for example, set parameters of a It follows from the above that the main point in certain model, while others perform the actual organizing the interaction between the M-function simulation. by the program in the language is the agreement of Naturally, the various data exchange between the types of data transferred and mapping of the set these functions is assumed. Inside the Matlab of output variables of M-function to the programming system, such kind of exchange can be corresponding variables of C ++ / C # program implemented using global variables, which could be that has been call and vice versa. To solve this declared in a Script-file. However, the third problem, it is proposed to use the .NET MWArray 34 class library for working with MWArray.dll arrays. container that can contain a MWArray or one of its This dynamic library contains two namespaces: derived types, including another MWCellArray; ● MathWorks.Matlab.NET.Arrays — classes MWCharArray is a managed representation that provide access to Matlab arrays from any .NET for Matlab arrays of character type. Like its CLS compliant language; equivalent Matlab, MWCharArray supports string ● MathWorks.Matlab.NET.Utility — service creation and string manipulation; classes that provide general support for MWArray MWIndexArray is an abstract class that serves classes in the Matlab MCR runtime enviroment. as the root for the MWArray indexed classes. These The Namespace MathWorks.Matlab.NET classes represent the types of arrays that can be .Arrays contains classes for supporting data used as input parameters for the array indexing conversion between managed types and Matlab operator []. types. Each class has constructors, destructors, and MWObjectArray is a special subclass of a set of properties and methods to access the state MWArray that encapsulates its own .NET object in of the Matlab main array. The class hierarchy of a Matlab array. This object can then be accessed or MathWorks.Matlab.NET .Arrays is shown in returned by the Matlab function. Figure 1. MWStructArray – managed represent-tation MWArray is an abstract class, the root of an for arrays of structures Matlab. Like its equivalent array class hierarchy Matlab. It encapsulates the in Matlab, it consists of field names and field internal type of Matlab mxArray and provides the values. ability to access, format and manage the array; Namespace MathWorks.MATLAB.NET. Utility. The service classes in this namespace provide general support for the MWArray classes in System.Object the execution environment of the MATLAB MCR component. MathWorks.MATLAB.NET.Arrays.MWArray The above class hierarchy makes it easy to convert data from the C ++ / C # format to the Matlab format and back, without resorting to MathWorks.MATLAB.NET.Arrays.MWNumericArray explicit type conversion. The following are examples of similar conversions. ● Variables of the double type do not require MathWorks.MATLAB.NET.Arrays.MWCellArray any conversions. ● One-dimensional array (vector) for transfer to M-function can be formed by the following MathWorks.MATLAB.NET.Arrays.MWLogicalArray operators: double [ ] Coeff = {1,2,3,4};//Vector in С++/С#; MathWorks.MATLAB.NET.Arrays.MWCharArray MWNumericArray MTLBCoeff = Coeff; // Vector to pass into M-function as a parameter. MathWorks.MATLAB.NET.Arrays.MWIndexArray ● Two-dimensional array for transfer into M - function can be formed by the following operators (*) and (**): MathWorks.MATLAB.NET.Arrays.MWObjectArray double [,] a ={ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // Matrix in C ++ / C #; (*) MWNumericArray arr1 = a;(**) //Matrix for MathWorks.MATLAB.NET.Arrays.MWStructArray transfer to M-function as a parameter. ● A string variable of type string is converted Figure 1: Class Hierarchy MathWorks to Matlab character format for transmission to the .Matlab.NET.Arrays.MWArray M-function using the following operators: MWNumericArray is a managed string My_String = " This is the C # "string; representation for Matlab arrays of numeric types. MWCharArray mw_ My_String = new Its Matlab-equivalent is the default array type MWCharArray(My_String); (vector) of double type, used by most Matlab math functions; ● Vector type MWArray, returned by M- MWLogicalArray is a managed representation function in C ++ / C # program; converted to an for Matlab boolean arrays. Like its equivalent array of type double using the following operators: Matlab, MWLogicalArray contains only ones and zeros (true / false); MWArray[] Res = null; MWCellArray is a managed representation for // Declaring MWArray Objects Matlab cell arrays. Each element in a cell array is a 35 MWNumericArray descriptor = null; // and Bey05]. Since, during data processing, it may be MWNumericArray necessary to filter their various variants with // select the first element from the MWArray different filters, it seems appropriate to organize the array and convert to input of a file name and coefficient vectors from an // numeric type MWNumericArray; application in C ++ / C #. descriptor = (MWNumericArray)Res[0]; Such an application should perform the // convert the array MWNumericArray to an following actions: array of type double; 1. Receive from the user the name of the file double[,]d_descriptor=(double[,]) descriptor. being processed in symbolic form and convert it to ToArray(MWArrayComponent.Real); the format adopted in Matlab; 2. Receive from the user a vector of filter Thus, the MWArray classes are used to process coefficients in a symbolic (user-friendly) form and arrays, determine the type, and convert the data. convert them to the format adopted in Matlab; The following are examples of data exchange 3. Create an object of the TFilter class between M-functions and C # applications. containing the filtering method. 4. Call the MainF method of the TFilter class 2 Application for Filtering Experimental and pass parameters (arguments) to it, which are converted to Matlab format. Data 5. Get the original and processed vector returned by M-function MainF, translating them Let there be a sequence of numbers xn, which is a into the format C #; (in type MWNumericArray – series of equidistant measurements of a certain controlled representation of types of Matlab quantity x (t), in which n is an integer (n = 0, 1, 2, numeric arrays.) ...), and t is considered as a continuous argument. If 6. Output the received vectors to the listBox the sequence yn is calculated by the formula: visual element (if necessary). ¥ Below are fragments of such an application y n = å ck xn -k ; that perform the above actions. k = -¥ // Declaring Variables then this formula defines a digital non-recursive string File_Name; // Name of data filter [Hem80]. file in С format MWArray [] Res = null; // It can be seen from the formula that the filter Declaring MWArray Class Objects operation is completely determined by the set of MWNumericArray Vect = null; // coefficients ck. and MWNumericArray for work results M-function MainF takes as parameters a set of // M-functions filter coefficients and the name of the binary file double [,] OutV; // Matrix into containing the data to be processed. which the work results will be converted The returned results are vectors containing the // M-functions source vector and the vector of processing results. string SCoeff; // C # format string for function [ OutMatrix ] = MainF(File,Coeff) entering filter coefficients %UNTITLED Summary of this function double [] Coeff; // Array (vector) goes here to be passed to M function % Detailed explanation goes here --------------------------------------------- % File - symbolic file name; Coeff is an // Converting the file name from the string array (vector) of coefficients. type to the Matlab character format fid2 = fopen(File,'r'); MWCharArray mw_Name = new InV = fread(fid2,'double'); % InV – source MWCharArray (File_Name); vector ------------------------------------------ fclose(fid2); Arg = length(InV); It was shown above that one of the parameters OutV = Filter (T,InV); % Appeal to M - transmitted to the M-function is an array (vector) of the function that implements the filter coefficients, which determines the properties of a % OutV – output vector digital filter. Their values are entered online, so the OutMatrix = [InV OutV']; % number of these coefficients is not known in Formation of a vector -lines of output advance. From the point of view of the M-function variables MainF, this does not matter, since all Matlab arrays plot(1:Arg,InV,1:Arg,OutV); % Plotting are by definition dynamic. However, in C # an results array is always a static structure, the size of which must be determined in advance. Therefore, the M-function MainF is declared as a CLR above transformations (*) and (**) cannot be used method of the TFilter class. It is not necessary to directly. To overcome this contradiction, a dynamic declare as the M- method the Filter function, since structure was created in an application in C # – a it is called from a function external to it [Smo05, 36 list, which is then converted into an array of the required size. ------------------------------------------------ List TMP = new List (); // Template for organizing the list // fill the list with entered coefficients // ................... // ................... Coeff = TMP.ToArray (); // Convert the list to an array MWNumericArray MTLBCoeff = Coeff; // Transform a numeric array from Figure 2: The result of the application working // C # format to a numeric array in the format // Matlab It should be noted that the use of listBox is TFilter Filter = new TFilter (); // Create an not the only option to visualize the results of the object of class TFilter application. Res = Filter.MainF (1, mw_Name, MTLBCoeff); // Call the MainF method 3 An Application for Determining the // select the first element from the MWArray Best access Point for Connecting a array and convert to Mobile Device to a Local Network // numeric type MWNumericArray; Vect = (MWNumericArray) Res [0]; In [Kra16], an algorithm was considered for int Size = Vect.NumberOfElements; determining the best access point for connecting a OutV = new double [Size / 2, Size / 2]; mobile device to a local network, taking into // Initialization of the result matrix account not only the signal level at its input, but for (int i = 0; i