Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How about using <a href="http://msdn.microsoft.com/en-us/library/8627sbea.aspx" rel="nofollow">.NET</a> <a href="http://www.mathworks.com/help/matlab/events-and-delegates.html" rel="nofollow">events</a> to inform listeners that an event occurred, where you <a href="http://www.mathworks.com/help/matlab/matlab_external/use-net-events-in-matlab.html" rel="nofollow">register an event handler in MATLAB</a> to do the actual printing.</p> <p>Here is a toy example that finds all prime numbers up to 10000. First we create the C# library:</p> <h2>MyClass.cs</h2> <pre><code>using System; namespace MyLibrary { public class MyClass { // function that does some work and notify listeners of occurred events public void FindPrimes() { // Primes between 1 and 10000 for (int i = 1; i &lt; 10000; i++) { if (MyClass.isPrime(i)) { //System.Console.WriteLine(i); onPrimeFound(i); } } } // helper function to determine if number is prime public static bool isPrime(int x) { if (x == 1) return false; if (x == 2) return true; for (int i = 2; i &lt;= Math.Ceiling(Math.Sqrt(x)); i++) { if (x % i == 0) return false; } return true; } // event broadcasted public event EventHandler PrimeFound; protected void onPrimeFound(int x) { var handler = this.PrimeFound; if (handler != null) { handler(this, new PrimeEventArgs(x)); } } } // event data passed to listeners public class PrimeEventArgs : EventArgs { public readonly int number; public PrimeEventArgs(int x) { this.number = x; } } } </code></pre> <h3>MATLAB</h3> <p>Next we use our class library inside MATLAB:</p> <pre><code>&gt;&gt; NET.addAssembly('C:\path\to\MyLibrary.dll'); &gt;&gt; c = MyLibrary.MyClass(); &gt;&gt; lh = addlistener(c, 'PrimeFound', @(o,e) fprintf('Prime: %d\n', e.number)); &gt;&gt; c.FindPrimes() Prime: 2 Prime: 3 Prime: 5 ... Prime: 9973 </code></pre> <p>The C# function <code>FindPrimes()</code> performs a lengthy operation, while emitting events to let interested observers know of occurred events (basically whenever you want to print something to MATLAB console). It should print immediately without buffering.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload