{"id":257,"date":"2010-12-07T21:12:22","date_gmt":"2010-12-07T21:12:22","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2010\/12\/07\/using-microsoft-net-to-expand-matlab-capabilities\/"},"modified":"2016-08-02T15:37:03","modified_gmt":"2016-08-02T20:37:03","slug":"using-microsoft-net-to-expand-matlab-capabilities","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2010\/12\/07\/using-microsoft-net-to-expand-matlab-capabilities\/","title":{"rendered":"Using Microsoft .NET to Expand MATLAB Capabilities"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><i>Ken Atwell in the MATLAB product management group is guest blogging this week about his recent experiences using Microsoft\r\n            .NET&reg; and how it can used to expand the breadth of capabilities in MATLAB, especially in situations where you need a facility\r\n            that MATLAB does not directly supply.<\/i><\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#2\">Introducing the problem to solve<\/a><\/li>\r\n         <li><a href=\"#3\">Finding a C#\/.NET solution<\/a><\/li>\r\n         <li><a href=\"#4\">Porting the code to MATLAB<\/a><\/li>\r\n         <li><a href=\"#5\">Refining the solution<\/a><\/li>\r\n         <li><a href=\"#6\">A second experiment<\/a><\/li>\r\n         <li><a href=\"#7\">Conclusion<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Microsoft .NET is a software framework for developing applications on Microsoft Windows.  Notably, it includes a comprehensive\r\n      library for all kinds of general-purpose programming tasks, from networking and security, to file parsing.  Since R2009a,\r\n       MATLAB has been able to call into the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/matlab_external\/brpb5k6.html\">.NET library<\/a>, but personally being a newbie to .NET, it was only very recently that I had enough motivation to invest it learning about\r\n      it.  It was a very positive experience, so I thought I&#8217;d share my story in this blog post.\r\n   <\/p>\r\n   <h3>Introducing the problem to solve<a name=\"2\"><\/a><\/h3>\r\n   <p>The other day, I was experimenting with the MATLAB function <tt>REGMATLABSERVER<\/tt>.  It registers MATLAB as an Automation server, so that it can be controlled by other applications.  For instance, this is\r\n      used by <a href=\"https:\/\/www.mathworks.com\/products\/excellink\">Spreadsheet Link EX<\/a> to enable live data transfer from Microsoft Excel to MATLAB (Even if you don&#8217;t know or care about what these things are,\r\n      bear with me, as much of what we&#8217;re discussing is generally applicable).  Under Windows 7 or Windows Vista, registering is\r\n      an Administrator-level operation that will cause User Account Control (UAC) to bring up a dialog box prompting the user to\r\n      confirm the action.  As it turns out, <tt>REGMATLABSERVER<\/tt> in its current form does not invoke UAC and will simply fail to register MATLAB. The work-around is easy enough -- run MATLAB\r\n      as an Administrator (right-click on the MATLAB Start Menu icon) and register from within that privileged MATLAB.  But, it\r\n      got me wondering, is there a way to invoke UAC from within MATLAB, avoiding the need to run MATLAB as a whole as an Administrator?\r\n   <\/p>\r\n   <p>Examining the implementation of <tt>REGMATLABSERVER<\/tt> shows that it works by simply invoking a second instance of MATLAB with a special command line switch.  These are the relevant\r\n      commands in <tt>REGMATLABSERVER<\/tt>:\r\n   <\/p><pre>  command = sprintf('\"%s\" \/wait \/regserver \/r quit', ...\r\n            fullfile(matlabroot,'bin','matlab'));\r\n  [s,msg] = system(command);<\/pre><h3>Finding a C#\/.NET solution<a name=\"3\"><\/a><\/h3>\r\n   <p>So, all I really needed to figure out was how to invoke a process <i>as an Administrator<\/i>. I knew of no way to do this in MATLAB, so like anyone, I used Google to find programmatic ways to start a process as an\r\n      Administrator.  I quickly learned that .NET provides facilities to do this, and found source code in C#\/.NET (on the <a href=\"http:\/\/en.wikipedia.org\/wiki\/User_Account_Control\">English-language Wikipedia page for UAC<\/a>, of all places) that demonstrates how to do it. At first, this gave me some pause, as I&#8217;d never used C# or .NET in any meaningful\r\n      way.  But, the code was very readable, so I decided to drop the code into MATLAB and see if I could make it work.  The C#\r\n      code from Wikipedia was:\r\n   <\/p><pre>  System.Diagnostics.Process proc = new System.Diagnostics.Process();\r\n  proc.StartInfo.FileName = \"C:\\\\Windows\\\\system32\\\\notepad.exe\";\r\n  proc.StartInfo.Verb = \"runas\"; \/\/ Elevate the application\r\n  proc.Start();<\/pre><h3>Porting the code to MATLAB<a name=\"4\"><\/a><\/h3>\r\n   <p>The first thing I needed to do was make some small syntax changes to turn this C# program into a MATLAB program.  With the\r\n      help of the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/matlab_env\/brqxeeu-151.html\">MATLAB Code Analyzer<\/a> (those wavy red and orange lines in the editor), I was able to get to valid MATLAB code in maybe a minute.  I needed to do\r\n      three things:\r\n   <\/p>\r\n   <li>The first line creates a variable of type <tt>System.Diagnostics.Process<\/tt>. I was not sure what that meant exactly, but I didn&#8217;t really need to know. MATLAB is implicitly typed, so the type name is\r\n      not needed in the variable declaration (that is, the left side of the <tt>=<\/tt>).  Further, the C# <tt>new<\/tt> keyword is not needed.\r\n   <\/li>\r\n   <li>The second and third lines needed their strings surrounded by single quotes instead of double quotes.<\/li>\r\n   <li>The comment on the third line needed to be preceded with <tt>%<\/tt> and not <tt>\/\/<\/tt>.\r\n   <\/li>\r\n   <p>This resulted in the following MATLAB code:<\/p><pre>  proc = System.Diagnostics.Process();\r\n  proc.StartInfo.FileName = 'C:\\\\Windows\\\\system32\\\\notepad.exe';\r\n  proc.StartInfo.Verb = 'runas'; % Elevate the application\r\n  proc.Start();<\/pre><p>Neat!  With very little effort, I had a script which launches the Notepad application as an Administrator, triggering UAC\r\n      prompts as necessary.  My next step was to use this as a starting point for what I actually want to do, namely start MATLAB\r\n      as an Administrator with extra command-line arguments (as we saw in the implementation of <tt>REGMATLABSERVER<\/tt> above). Given that I now knew the name of the .NET class I needed (<tt>System.Diagnostics.Process<\/tt>), I was able to get to the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.diagnostics.process.aspx\">MSDN reference page<\/a> (again, Google is your friend).  Between this reference page and tab completion in MATLAB (type <tt>proc.<\/tt> , then press the Tab key), I was able to poke around and specify the bits I needed:\r\n   <\/p><pre>  proc = System.Diagnostics.Process;\r\n  % EXE to run\r\n  proc.StartInfo.FileName = fullfile(matlabroot,'bin','matlab');\r\n  % Arguments to the EXE\r\n  proc.StartInfo.Arguments = '\/wait \/regserver \/r quit';\r\n  % Run-as admin\r\n  proc.StartInfo.Verb = 'runas';\r\n  proc.Start(); % Start<\/pre><h3>Refining the solution<a name=\"5\"><\/a><\/h3>\r\n   <p>It was now working, but there were two things I didn&#8217;t like:<\/p>\r\n   <li>After spawning the process, control was immediately returned to MATLAB, but I wanted MATLAB to block until the spawned process\r\n      finished so I could examine the exit code.\r\n   <\/li>\r\n   <li>There was an empty Command Prompt window visible on the screen for a couple of seconds.  Cosmetic, to be sure, but ugly.<\/li>\r\n   <p>Another several more minutes of experimentation and iteration, and I got to a more satisfying implementation:<\/p><pre>  proc = System.Diagnostics.Process;\r\n  % EXE to run\r\n  proc.StartInfo.FileName = fullfile(matlabroot,'bin','matlab');\r\n  % Arguments to the EXE\r\n  proc.StartInfo.Arguments = '\/wait \/regserver \/r quit';\r\n  % Run-as admin\r\n  proc.StartInfo.Verb = 'runas';\r\n  proc.StartInfo.WindowStyle = ...\r\n        System.Diagnostics.ProcessWindowStyle.Hidden;\r\n  proc.Start(); % Start\r\n  proc.WaitForExit(); % Wait for the process to end\r\n  proc.ExitCode %Display exit code<\/pre><p>Not bad!  Knowing next-to-nothing about .NET, it took me about a half-hour to create my first MATLAB script that leverages\r\n      .NET.  Granted, this is a trivial program, but it does something new and useful.  I know this sounds corny, but it was something\r\n      of a revelatory moment for me and I won&#8217;t hesitate to leverage .NET when the situation next arises.\r\n   <\/p>\r\n   <h3>A second experiment<a name=\"6\"><\/a><\/h3>\r\n   <p>For a second experiment, I recalled hearing somewhere that .NET includes a speech synthesizer. I thought this might be a &#8220;fun&#8221;\r\n      example, so I Google searched &#8220;.NET speech synthesis&#8221; and found a <a href=\"http:\/\/www.codeproject.com\/KB\/dotnet\/Speech_Sample.aspx\">nice article on the subject<\/a>, with code in C++, C#, and VB.  Reading through the short article, it said something about needing to &#8220;reference the <tt>System.Speech<\/tt> assembly&#8221;.  I was not quite sure what that meant, so I moved on.  The C# code looked closest to MATLAB code, so I started\r\n      with it:\r\n   <\/p><pre>  using System.Speech.Synthesis;\r\n  SpeechSynthesizer speaker = new SpeechSynthesizer();\r\n  speaker.Rate = 1;\r\n  speaker.Volume = 100;\r\n  speaker.Speak(\"Hello world.\");<\/pre><p>As before, I tweaked things a tiny bit to make the syntax agreeable to MATLAB:<\/p><pre>  using System.Speech.Synthesis;\r\n  speaker = SpeechSynthesizer();\r\n  speaker.Rate = 1;\r\n  speaker.Volume = 100;\r\n  speaker.Speak('Hello world.');<\/pre><p>Here, MATLAB (perhaps unsurprisingly) generated an error on the <tt>using<\/tt> keyword.  Being a past C++ programmer, I recognized the <tt>using<\/tt> statement as a way to bring a namespace into scope; stated more pragmatically, it is a mechanism to save keystrokes later\r\n      in your code. I could avoid the need for a <tt>using<\/tt> statement by simply specifying the full &#8220;path&#8221; to <tt>SpeechSynthesizer<\/tt> instead:\r\n   <\/p><pre>  speaker = System.Speech.Synthesis.SpeechSynthesizer();\r\n  speaker.Rate = 1;\r\n  speaker.Volume = 100;\r\n  speaker.Speak('Hello world.');<\/pre><p>But, that still didn&#8217;t work, as I triggered an error about an undefined variable or class. Hmm&#8230; guess I needed to pay attention\r\n      earlier when the article advised me to add a reference to the <tt>System.Speech<\/tt> assembly.  I did not know how to do this in MATLAB, so I Google searched &#8220;MATLAB add reference to .NET assembly&#8221;.  For me,\r\n      the first couple of results were from mathworks.com.  The first result was an index of functions; the second was the topic\r\n      <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/matlab_external\/brpcay8-1.html\">Getting Started with .NET<\/a>. Reading through the first few paragraphs of the introduction, I learned that in MATLAB I need to use <tt><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/net.addassembly.html\">NET.addAssembly<\/a><\/tt> to load an assembly.  It is analogous to using <tt>JAVAADDPATH<\/tt> to make Java classes visible to MATLAB.  As it turns out, a few particularly useful assemblies are loaded by default, which\r\n      explains why I did not need to bother with this in the first example (basically, I got lucky!).\r\n   <\/p>\r\n   <p>Armed with this information, I inserted a line of code to add the assembly before using its classes:<\/p><pre>  NET.addAssembly('System.Speech');\r\n  speaker = System.Speech.Synthesis.SpeechSynthesizer();\r\n  speaker.Rate = 1;\r\n  speaker.Volume = 100;\r\n  speaker.Speak('Hello world.');<\/pre><p>And <i>voil&agrave;<\/i>!  My computer was now saying &#8220;Hello world&#8221; to me.  Since I was having fun, I changed the message to tell me the day of the\r\n      week:\r\n   <\/p><pre>  NET.addAssembly('System.Speech');\r\n  speaker = System.Speech.Synthesis.SpeechSynthesizer();\r\n  speaker.Rate = 1;\r\n  speaker.Volume = 100;\r\n  [~,S]=weekday(date, 'long');\r\n  speaker.Speak(['Today is ' S]);<\/pre><p>I now had my second MATLAB program using .NET.  This one was a touch more complex, as I need to understand <tt>NET.addAssembly<\/tt>, but that was a hurdle I was able to get over with little fuss.\r\n   <\/p>\r\n   <h3>Conclusion<a name=\"7\"><\/a><\/h3>\r\n   <p>Bottom line:  When you&#8217;re looking to solve a problem that is outside the normal purview of MATLAB, don&#8217;t underestimate the\r\n      power of the .NET Framework you have at your fingertips (or, on the off chance .NET is not already installed, it is freely\r\n      available for download from Microsoft). Further, don&#8217;t be daunted by .NET if you&#8217;ve never used it, as starting points (code fragments) are easy to come by and\r\n      C# and other languages can often easily be ported into MATLAB.\r\n   <\/p>\r\n   <p>I&#8217;m curious to hear if any readers have had experiences with .NET in MATLAB.  What kinds of problems do you solve?  Chime\r\n      in <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=257#respond\">below<\/a>!\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_5f50a186e4f547debf9a136909af155f() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='5f50a186e4f547debf9a136909af155f ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 5f50a186e4f547debf9a136909af155f';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Ken Atwell';\r\n        copyright = 'Copyright 2010 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_5f50a186e4f547debf9a136909af155f()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.11<br><\/p>\r\n<\/div>\r\n<!--\r\n5f50a186e4f547debf9a136909af155f ##### SOURCE BEGIN #####\r\n%% Using Microsoft .NET to Expand MATLAB Capabilities\r\n%\r\n% _Ken Atwell in the MATLAB product management group is guest blogging this\r\n% week about his recent experiences using Microsoft .NET\u00c2\u00ae and how it can\r\n% used to expand the breadth of capabilities in MATLAB, especially in\r\n% situations where you need a facility that MATLAB does not directly\r\n% supply._\r\n%%\r\n% Microsoft .NET is a software framework for developing applications on\r\n% Microsoft Windows.  Notably, it includes a comprehensive library for all\r\n% kinds of general-purpose programming tasks, from networking and security,\r\n% to file parsing.  Since R2009a,  MATLAB has been able to call into the\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/matlab_external\/brpb5k6.html .NET\r\n% library>, but personally being a newbie to .NET, it was only very\r\n% recently that I had enough motivation to invest it learning about it.  It\r\n% was a very positive experience, so I thought I\u00e2\u20ac\u2122d share my story in this\r\n% blog post.\r\n%\r\n%% Introducing the problem to solve\r\n%\r\n% The other day, I was experimenting with the MATLAB function\r\n% |REGMATLABSERVER|.  It registers MATLAB as an Automation server, so that\r\n% it can be controlled by other applications.  For instance, this is used\r\n% by <https:\/\/www.mathworks.com\/products\/excellink Spreadsheet Link EX> to\r\n% enable live data transfer from Microsoft Excel to MATLAB (Even if you\r\n% don\u00e2\u20ac\u2122t know or care about what these things are, bear with me, as much of\r\n% what we\u00e2\u20ac\u2122re discussing is generally applicable).  Under Windows 7 or\r\n% Windows Vista, registering is an Administrator-level operation that will\r\n% cause User Account Control (UAC) to bring up a dialog box prompting the\r\n% user to confirm the action.  As it turns out, |REGMATLABSERVER| in its\r\n% current form does not invoke UAC and will simply fail to register MATLAB.\r\n% The work-around is easy enough REPLACE_WITH_DASH_DASH run MATLAB as an Administrator\r\n% (right-click on the MATLAB Start Menu icon) and register from within that\r\n% privileged MATLAB.  But, it got me wondering, is there a way to invoke\r\n% UAC from within MATLAB, avoiding the need to run MATLAB as a whole as an\r\n% Administrator?\r\n%\r\n% Examining the implementation of |REGMATLABSERVER| shows that it works by\r\n% simply invoking a second instance of MATLAB with a special command line\r\n% switch.  These are the relevant commands in |REGMATLABSERVER|:\r\n%\r\n%    command = sprintf('\"%s\" \/wait \/regserver \/r quit', ...\r\n%              fullfile(matlabroot,'bin','matlab'));\r\n%    [s,msg] = system(command);\r\n% \r\n%% Finding a C#\/.NET solution\r\n%\r\n% So, all I really needed to figure out was how to invoke a process _as an\r\n% Administrator_. I knew of no way to do this in MATLAB, so like anyone, I\r\n% used Google to find programmatic ways to start a process as an\r\n% Administrator.  I quickly learned that .NET provides facilities to do\r\n% this, and found source code in C#\/.NET (on the\r\n% <http:\/\/en.wikipedia.org\/wiki\/User_Account_Control English-language\r\n% Wikipedia page for UAC>, of all places) that demonstrates how to do it.\r\n% At first, this gave me some pause, as I\u00e2\u20ac\u2122d never used C# or .NET in any\r\n% meaningful way.  But, the code was very readable, so I decided to drop\r\n% the code into MATLAB and see if I could make it work.  The C# code from\r\n% Wikipedia was:\r\n%\r\n%    System.Diagnostics.Process proc = new System.Diagnostics.Process();\r\n%    proc.StartInfo.FileName = \"C:\\\\Windows\\\\system32\\\\notepad.exe\";\r\n%    proc.StartInfo.Verb = \"runas\"; \/\/ Elevate the application\r\n%    proc.Start();\r\n%\r\n%% Porting the code to MATLAB\r\n%\r\n% The first thing I needed to do was make some small syntax changes to turn\r\n% this C# program into a MATLAB program.  With the help of the\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/matlab_env\/brqxeeu-151.html MATLAB\r\n% Code Analyzer> (those wavy red and orange lines in the editor), I was\r\n% able to get to valid MATLAB code in maybe a minute.  I needed to do three\r\n% things:\r\n%\r\n% # The first line creates a variable of type |System.Diagnostics.Process|.\r\n% I was not sure what that meant exactly, but I didn\u00e2\u20ac\u2122t really need to know.\r\n% MATLAB is implicitly typed, so the type name is not needed in the\r\n% variable declaration (that is, the left side of the |=|).  Further, the\r\n% C# |new| keyword is not needed.\r\n% # The second and third lines needed their strings surrounded by single\r\n% quotes instead of double quotes.\r\n% # The comment on the third line needed to be preceded with |%| and not\r\n% |\/\/|.\r\n%\r\n% This resulted in the following MATLAB code:\r\n%\r\n%    proc = System.Diagnostics.Process();\r\n%    proc.StartInfo.FileName = 'C:\\\\Windows\\\\system32\\\\notepad.exe';\r\n%    proc.StartInfo.Verb = 'runas'; % Elevate the application\r\n%    proc.Start();\r\n%\r\n% Neat!  With very little effort, I had a script which launches the Notepad\r\n% application as an Administrator, triggering UAC prompts as necessary.  My\r\n% next step was to use this as a starting point for what I actually want to\r\n% do, namely start MATLAB as an Administrator with extra command-line\r\n% arguments (as we saw in the implementation of |REGMATLABSERVER| above).\r\n% Given that I now knew the name of the .NET class I needed\r\n% (|System.Diagnostics.Process|), I was able to get to the\r\n% <http:\/\/msdn.microsoft.com\/en-us\/library\/system.diagnostics.process.aspx\r\n% MSDN reference page> (again, Google is your friend).  Between this\r\n% reference page and tab completion in MATLAB (type |proc.| , then press\r\n% the Tab key), I was able to poke around and specify the bits I needed:\r\n%\r\n%    proc = System.Diagnostics.Process;\r\n%    % EXE to run\r\n%    proc.StartInfo.FileName = fullfile(matlabroot,'bin','matlab');\r\n%    % Arguments to the EXE\r\n%    proc.StartInfo.Arguments = '\/wait \/regserver \/r quit'; \r\n%    % Run-as admin\r\n%    proc.StartInfo.Verb = 'runas'; \r\n%    proc.Start(); % Start\r\n%\r\n%% Refining the solution\r\n%\r\n% It was now working, but there were two things I didn\u00e2\u20ac\u2122t like:\r\n%\r\n% # After spawning the process, control was immediately returned to MATLAB,\r\n% but I wanted MATLAB to block until the spawned process finished so I\r\n% could examine the exit code.\r\n% # There was an empty Command Prompt window visible on the screen for a\r\n% couple of seconds.  Cosmetic, to be sure, but ugly.\r\n%\r\n% Another several more minutes of experimentation and iteration, and I got\r\n% to a more satisfying implementation:\r\n%\r\n%    proc = System.Diagnostics.Process;\r\n%    % EXE to run\r\n%    proc.StartInfo.FileName = fullfile(matlabroot,'bin','matlab');\r\n%    % Arguments to the EXE\r\n%    proc.StartInfo.Arguments = '\/wait \/regserver \/r quit'; \r\n%    % Run-as admin\r\n%    proc.StartInfo.Verb = 'runas'; \r\n%    proc.StartInfo.WindowStyle = ... \r\n%          System.Diagnostics.ProcessWindowStyle.Hidden;\r\n%    proc.Start(); % Start\r\n%    proc.WaitForExit(); % Wait for the process to end\r\n%    proc.ExitCode %Display exit code\r\n% \r\n% Not bad!  Knowing next-to-nothing about .NET, it took me about a\r\n% half-hour to create my first MATLAB script that leverages .NET.  Granted,\r\n% this is a trivial program, but it does something new and useful.  I know\r\n% this sounds corny, but it was something of a revelatory moment for me and\r\n% I won\u00e2\u20ac\u2122t hesitate to leverage .NET when the situation next arises.\r\n%\r\n%% A second experiment\r\n%\r\n% For a second experiment, I recalled hearing somewhere that .NET includes\r\n% a speech synthesizer. I thought this might be a \u00e2\u20ac\u0153fun\u00e2\u20ac\ufffd example, so I\r\n% Google searched \u00e2\u20ac\u0153.NET speech synthesis\u00e2\u20ac\ufffd and found a\r\n% <http:\/\/www.codeproject.com\/KB\/dotnet\/Speech_Sample.aspx nice article on\r\n% the subject>, with code in C++, C#, and VB.  Reading through the short\r\n% article, it said something about needing to \u00e2\u20ac\u0153reference the\r\n% |System.Speech| assembly\u00e2\u20ac\ufffd.  I was not quite sure what that meant, so I\r\n% moved on.  The C# code looked closest to MATLAB code, so I started with\r\n% it:\r\n%\r\n%    using System.Speech.Synthesis;\r\n%    SpeechSynthesizer speaker = new SpeechSynthesizer();\r\n%    speaker.Rate = 1;\r\n%    speaker.Volume = 100;\r\n%    speaker.Speak(\"Hello world.\");\r\n% \r\n% As before, I tweaked things a tiny bit to make the syntax agreeable to\r\n% MATLAB:\r\n%\r\n%    using System.Speech.Synthesis;\r\n%    speaker = SpeechSynthesizer();\r\n%    speaker.Rate = 1;\r\n%    speaker.Volume = 100;\r\n%    speaker.Speak('Hello world.');\r\n% \r\n% Here, MATLAB (perhaps unsurprisingly) generated an error on the |using|\r\n% keyword.  Being a past C++ programmer, I recognized the |using| statement\r\n% as a way to bring a namespace into scope; stated more pragmatically, it\r\n% is a mechanism to save keystrokes later in your code. I could avoid the\r\n% need for a |using| statement by simply specifying the full \u00e2\u20ac\u0153path\u00e2\u20ac\ufffd to\r\n% |SpeechSynthesizer| instead:\r\n%\r\n%    speaker = System.Speech.Synthesis.SpeechSynthesizer();\r\n%    speaker.Rate = 1;\r\n%    speaker.Volume = 100;\r\n%    speaker.Speak('Hello world.');\r\n% \r\n% But, that still didn\u00e2\u20ac\u2122t work, as I triggered an error about an undefined\r\n% variable or class. Hmm\u00e2\u20ac\u00a6 guess I needed to pay attention earlier when the\r\n% article advised me to add a reference to the |System.Speech| assembly.  I\r\n% did not know how to do this in MATLAB, so I Google searched \u00e2\u20ac\u0153MATLAB add\r\n% reference to .NET assembly\u00e2\u20ac\ufffd.  For me, the first couple of results were\r\n% from mathworks.com.  The first result was an index of functions; the\r\n% second was the topic\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/matlab_external\/brpcay8-1.html\r\n% Getting Started with .NET>. Reading through the first few paragraphs of\r\n% the introduction, I learned that in MATLAB I need to use\r\n% |<https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/net.addassembly.html\r\n% NET.addAssembly>| to load an assembly.  It is analogous to using\r\n% |JAVAADDPATH| to make Java classes visible to MATLAB.  As it turns out, a\r\n% few particularly useful assemblies are loaded by default, which explains\r\n% why I did not need to bother with this in the first example (basically, I\r\n% got lucky!).\r\n%\r\n% Armed with this information, I inserted a line of code to add the\r\n% assembly before using its classes:\r\n%\r\n%    NET.addAssembly('System.Speech');\r\n%    speaker = System.Speech.Synthesis.SpeechSynthesizer();\r\n%    speaker.Rate = 1;\r\n%    speaker.Volume = 100;\r\n%    speaker.Speak('Hello world.');\r\n% \r\n% And _voil\u00c3\u00a0_!  My computer was now saying \u00e2\u20ac\u0153Hello world\u00e2\u20ac\ufffd to me.  Since I\r\n% was having fun, I changed the message to tell me the day of the week:\r\n%\r\n%    NET.addAssembly('System.Speech');\r\n%    speaker = System.Speech.Synthesis.SpeechSynthesizer();\r\n%    speaker.Rate = 1;\r\n%    speaker.Volume = 100;\r\n%    [~,S]=weekday(date, 'long');\r\n%    speaker.Speak(['Today is ' S]);\r\n% \r\n% I now had my second MATLAB program using .NET.  This one was a touch more\r\n% complex, as I need to understand |NET.addAssembly|, but that was a hurdle\r\n% I was able to get over with little fuss.\r\n%\r\n%% Conclusion\r\n%\r\n% Bottom line:  When you\u00e2\u20ac\u2122re looking to solve a problem that is outside the\r\n% normal purview of MATLAB, don\u00e2\u20ac\u2122t underestimate the power of the .NET\r\n% Framework you have at your fingertips (or, on the off chance .NET is not\r\n% already installed, it is freely available for\r\n% <http:\/\/www.microsoft.com\/net\/download.aspx download from Microsoft>).\r\n% Further, don\u00e2\u20ac\u2122t be daunted by .NET if you\u00e2\u20ac\u2122ve never used it, as starting\r\n% points (code fragments) are easy to come by and C# and other languages\r\n% can often easily be ported into MATLAB.\r\n%\r\n% I\u00e2\u20ac\u2122m curious to hear if any readers have had experiences with .NET in\r\n% MATLAB.  What kinds of problems do you solve?  Chime in\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=257#respond below>!\r\n% \r\n\r\n\r\n##### SOURCE END ##### 5f50a186e4f547debf9a136909af155f\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Ken Atwell in the MATLAB product management group is guest blogging this week about his recent experiences using Microsoft\r\n            .NET&reg; and how it can used to expand the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2010\/12\/07\/using-microsoft-net-to-expand-matlab-capabilities\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[42,39,15],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/257"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=257"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/257\/revisions"}],"predecessor-version":[{"id":1899,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/257\/revisions\/1899"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}