2

I have a c# system which has lots of time-series information. Each information is stored in the following data-structure :

public struct Tag
{
   DateTime Time {get;set;}
   double Value {get;set;}
}

We can actually consider these arrays of tags as Matlab vectors:

Array of Tags:

Time  |  Value
...   |  ...
...   |  ...
...   |  ...

Ok, so what I want is to provide the user with a very easy UI which he can write manipulations and expressions on these tags. The best way I came up with, is to let the user write Matlab expressions on these tags (This way the user is able to do almost anything with these vectors, he won't be needed to learn a new language, and the performance will be very fast - I forgot to mention, my Tag arrays are really big)

For example:

Let's say we have a tempratureTagArray and a windTagArray stored in the current run. (Each of these are arrays with minute-values between sunday and monday).

So I want the user to write something like:

x = windTagArray;
y = tempratureTagArray;

And then, a matlab expression :

if (x > y)
    result = x.^y + y./7 * 12;
else
    result = exp(x) + y.^6 - 7 + (x.*y).^3 + log(y);
end

I will take care for the part of defining the vectors (x and y) in my c# program and then I'll create a matlab .m file from this. What I need is an easy way to launch these .m files on matlab, and get the result vectors from these .m files in my c# runtime. This seems to me the easiest way to deal with such problem (I also wonder if this way is fast enough)

So, does anyone have a suggestion for implementing such operation?

I thought about launching a background Matlab program instance, and for every request, somehow "mock" user operations on the Matlab instance and then somehow read the results. But I am not sure its the best way for doing it. I wonder if maybe someone had to deal with such scenario.

Thanks a lot.

Edit: I would also be happy to hear your thoughts about runtime of such operations. This thing is going to run on a server with matlab which will get lots of such requests. I actually thought about Matlab because I know it works really fast when you use its native application (and it supports lots of math operations of course)

user2630165
  • 311
  • 3
  • 10

2 Answers2

1

Please try to follow below examples:

example 1

example 2

example 3

It is also important to know that you are interested in calculation or you want to generate the m file as well.

Community
  • 1
  • 1
User1551892
  • 3,236
  • 8
  • 32
  • 52
  • Hi. Thanks a lot for your answer. I'm actually interested only with executing the .m file (the creation of the .m file isn't a complex problem) and get the result in my c# program. – user2630165 Oct 29 '15 at 10:28
1

I faced a similar scenario and I came up with the following solution. Obviously it's not the only solution, but it works for me.

  1. Write the variables to a binary/text file you can read from Matlab.
  2. Generate the Matlab code as a string in your c# code. It should be of course a valid Matlab expression, but without newlines in the string. use , or ; to separate commands. The code should include the reading of the variables from the binary/text file.
  3. Wrap your code with a try-catch, so your call will not hang on an error if it occurs. Append an exit command to close Matlab.
  4. The Matlab command should include a save of the result to a binary/text file which you can read from c#.
  5. From your code you should issue a call to Matlab (similar to using system command from Matlab), using the -r option which enables you to issue the string as an input (e.g, see here)
  6. In your c# code, use a while to wait for the result file to be generated. Use some maximal wait time to avoid infinite while. After the result file is generated, read the result from the saved file.
Itamar Katz
  • 9,544
  • 5
  • 42
  • 74
  • Hi. Thanks for your answer. Do you know how fast this thing is going to work? Am I going to loose time because I'm not using the native Matlab application but only calling to Matlab functions from outside ? I'm asking because I'm going to perform LOTS of such operations with HUGE vectors every day – user2630165 Oct 29 '15 at 10:34
  • I am not sure my approach scales up in a good way, since every call to `matlab -r` is like executing matlab, so it takes a few seconds to load. If you have many calls, this overhead might be too much. – Itamar Katz Oct 29 '15 at 13:50
  • Maybe using sockets is a better solution (which I don't know enough about) – Itamar Katz Oct 29 '15 at 14:13