{"id":260,"date":"2011-01-06T13:20:42","date_gmt":"2011-01-06T13:20:42","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2011\/01\/06\/matlab-data-types-as-arguments-to-standalone-applications\/"},"modified":"2016-08-04T08:37:56","modified_gmt":"2016-08-04T13:37:56","slug":"matlab-data-types-as-arguments-to-standalone-applications","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2011\/01\/06\/matlab-data-types-as-arguments-to-standalone-applications\/","title":{"rendered":"MATLAB Data Types as Arguments to Standalone Applications"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Guest blogger <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/4660\">Peter Webb<\/a> returns with another in an <a href=\"https:\/\/blogs.mathworks.com\/loren\/category\/deployment\/\">occasional series<\/a> of postings about application deployment.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#2\">MATLAB-Type Arguments<\/a><\/li>\r\n         <li><a href=\"#4\">Your Arguments Don't Have to be Strings<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>When you create a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/compiler\/f7-996249.html\"><i>standalone executable<\/i><\/a> from a MATLAB function with <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/compiler\/mcc.html\">MATLAB Compiler<\/a>, the generated main program has a limited ability to process command line arguments as strings. In a previous posting, I\r\n      presented techniques to handle string arguments and to turn them into numeric scalars. This week, I'll show you how to handle\r\n      more complex arguments in your standalone executables.\r\n   <\/p>\r\n   <h3>MATLAB-Type Arguments<a name=\"2\"><\/a><\/h3>\r\n   <p>MATLAB's data types encompass far more than strings and scalars. This example demonstrates how to pass cell arrays and function\r\n      handles to a standalone executable.\r\n   <\/p>\r\n   <p>The function <tt>cellops<\/tt> takes two inputs: a cell array of data and a function to apply to each cell in the cell array. <tt>cellops<\/tt> calls <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/cellfun.html\"><tt>cellfun<\/tt><\/a> twice: once to apply the function to the cell array and a second time to display the results.\r\n   <\/p>\r\n   <p>This function works as a standalone executable because it calls <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/eval.html\"><tt>eval<\/tt><\/a> on its inputs if they are strings. Therefore, the strings on the command line must be valid MATLAB expressions.\r\n   <\/p><pre>function cellops(celldata, fcn)\r\n  % If either if the inputs is a string, assume it is a MATLAB\r\n  % expression, and call EVAL on it.\r\n  if ischar(celldata)\r\n      celldata = eval(celldata);\r\n  end<\/pre><pre>  if ischar(fcn)\r\n      fcn = eval(fcn);\r\n  end<\/pre><pre>  % Apply the function to the cell array.\r\n  output = cellfun(fcn, celldata, 'UniformOutput', false);<\/pre><pre>  % Display all the results.\r\n  cellfun(@disp, output);<\/pre><p>Compile the MATLAB function:<\/p><pre>mcc -mv cellops<\/pre><p>Then, pass <tt>cellops<\/tt> a cell array and a function handle, as in the following two examples.\r\n   <\/p>\r\n   <p><i>Example 1<\/i><\/p>\r\n   <p>Use <tt>cellops<\/tt> to compute the mean of two random datasets. The first argument is a MATLAB expression that creates a cell array containing\r\n      two vectors of random numbers, and the second argument creates a MATLAB function handle for the function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/mean.html\"><tt>mean<\/tt><\/a>.\r\n   <\/p><pre>cellops \"{randn(10,1), randn(20,1)}\" \"@mean\"<\/pre><p><i>Example 2<\/i><\/p>\r\n   <p>Search for positive values in three datasets. The first argument is a cell array containing the three datasets -- three vectors\r\n      of random numbers -- and the second is an <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/matlab_prog\/f4-70115.html\">anonymous function<\/a> that extracts the positive numbers from its input.\r\n   <\/p><pre>cellops \"{randn(10,1), randn(20,1), randn(30,1)}\" \"@(x) x(x&gt;0)\"<\/pre><p>Notice the arguments to <tt>cellops<\/tt> must be MATLAB expressions with non-empty results. If you enter a statement, like <tt>x = {23, 17, 81}<\/tt>, <tt>eval<\/tt> will process it, but since MATLAB statements have no result, you'll receive an error.\r\n   <\/p><pre> cellops \"x={23, 17, 81}\" \"@mean\"<\/pre><pre> ??? Error: The expression to the left of the equals sign is not a valid\r\n     target for an assignment.\r\n Error in ==&gt; cellops at 5<\/pre><p>The first call to <tt>eval<\/tt>, <tt>celldata = eval(celldata);<\/tt>, generated this error. It isn't possible to assign the non-existant result of <tt>eval<\/tt> to the variable <tt>celldata<\/tt>.\r\n   <\/p>\r\n   <p>The second argument to <tt>cellops<\/tt> is a function handle. When creating function handles with <tt>eval<\/tt>, you must ensure that your function handles refer to functions included in the deployed application. Generally speaking,\r\n      this means you can use <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/builtin.html\">builtin functions<\/a>, functions in the MATLAB toolbox, anonymous functions and functions you've explicitly added with <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/compiler\/mcc.html\"><tt>-a<\/tt><\/a>. I'll discuss this point in a later post on the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/depfun.html\"><tt>depfun<\/tt><\/a> function.\r\n   <\/p>\r\n   <h3>Your Arguments Don't Have to be Strings<a name=\"4\"><\/a><\/h3>\r\n   <p>I hope these examples demonstrate how to increase the sophistication of your standalone executables' argument processing.\r\n      Even though standalone executables only receive strings from the command line, you can turn those strings into many different\r\n      kinds of data with a few extra lines of MATLAB code.\r\n   <\/p>\r\n   <p>I leave you with exercise for the reader: how would you pass a MATLAB structure to a standalone executable? I have a few ideas,\r\n      but none of them are really elegant.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_e650b7cc174845fcb9ef3a86b041287f() {\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='e650b7cc174845fcb9ef3a86b041287f ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e650b7cc174845fcb9ef3a86b041287f';\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 = 'Peter Webb';\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_e650b7cc174845fcb9ef3a86b041287f()\"><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\ne650b7cc174845fcb9ef3a86b041287f ##### SOURCE BEGIN #####\r\n%% MATLAB Data Types as Arguments to Standalone Applications\r\n% Guest blogger \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/4660 Peter Webb>\r\n% returns with another in an \r\n% <https:\/\/blogs.mathworks.com\/loren\/category\/deployment\/ occasional series>\r\n% of postings about application deployment. \r\n\r\n%%\r\n% When you create a \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/compiler\/f7-996249.html \r\n% _standalone executable_> from a MATLAB function with \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/compiler\/mcc.html \r\n% MATLAB Compiler>, the generated main program has a limited ability to\r\n% process command line arguments as strings. In a previous posting, I\r\n% presented techniques to handle string arguments and to turn them into\r\n% numeric scalars. This week, I'll show you how to handle more complex \r\n% arguments in your standalone executables.\r\n%\r\n%% MATLAB-Type Arguments\r\n% MATLAB's data types encompass far more than strings and scalars. This\r\n% example demonstrates how to pass cell arrays and function handles to\r\n% a standalone executable. \r\n%\r\n% The function |cellops| takes two inputs: a cell array of data and a\r\n% function to apply to each cell in the cell array. |cellops| calls \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/cellfun.html\r\n% |cellfun|> twice: once to apply the function to the cell array and a \r\n% second time to display the results.\r\n%\r\n% This function works as a standalone executable because it calls \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/eval.html\r\n% |eval|> on its inputs if they are strings. Therefore, the strings on the\r\n% command line must be valid MATLAB expressions. \r\n%\r\n%  function cellops(celldata, fcn)\r\n%    % If either if the inputs is a string, assume it is a MATLAB\r\n%    % expression, and call EVAL on it.\r\n%    if ischar(celldata)\r\n%        celldata = eval(celldata);\r\n%    end\r\n%    \r\n%    if ischar(fcn)\r\n%        fcn = eval(fcn);\r\n%    end\r\n%    \r\n%    % Apply the function to the cell array.\r\n%    output = cellfun(fcn, celldata, 'UniformOutput', false);\r\n%    \r\n%    % Display all the results.\r\n%    cellfun(@disp, output);\r\n \r\n%%\r\n% Compile the MATLAB function:\r\n%\r\n%  mcc -mv cellops\r\n%\r\n% Then, pass |cellops| a cell array and a function handle, as in the\r\n% following two examples.\r\n%\r\n% _Example 1_\r\n%\r\n% Use |cellops| to compute the mean of two random datasets. \r\n% The first argument is a MATLAB\r\n% expression that creates a cell array containing two vectors of random\r\n% numbers, and the second argument creates a MATLAB function handle for the\r\n% function \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/mean.html\r\n% |mean|>.\r\n%\r\n%  cellops \"{randn(10,1), randn(20,1)}\" \"@mean\"\r\n%\r\n% _Example 2_\r\n% \r\n% Search for positive values in three datasets. The first argument is\r\n% a cell array containing the three datasets REPLACE_WITH_DASH_DASH three vectors of random\r\n% numbers REPLACE_WITH_DASH_DASH and the second is an \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/matlab_prog\/f4-70115.html\r\n% anonymous function> that extracts the positive numbers from its input.\r\n% \r\n%  cellops \"{randn(10,1), randn(20,1), randn(30,1)}\" \"@(x) x(x>0)\"\r\n%\r\n% Notice the arguments to |cellops| must be MATLAB expressions with\r\n% non-empty results. If you enter a statement, like |x = {23, 17, 81}|,\r\n% |eval| will process it, but since MATLAB statements have no result,\r\n% you'll receive an error.\r\n%\r\n%   cellops \"x={23, 17, 81}\" \"@mean\"\r\n%\r\n%   ??? Error: The expression to the left of the equals sign is not a valid \r\n%       target for an assignment.\r\n%   Error in ==> cellops at 5\r\n%\r\n% The first call to |eval|, |celldata = eval(celldata);|, generated this\r\n% error. It isn't possible to assign the non-existant result of\r\n% |eval| to the variable |celldata|.\r\n%\r\n% The second argument to |cellops| is a function handle. \r\n% When creating function handles with |eval|, you must ensure that your \r\n% function handles refer to functions included in the deployed application. \r\n% Generally speaking, this means you can use\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/builtin.html \r\n% builtin functions>, functions in the MATLAB toolbox, anonymous functions\r\n% and functions you've explicitly added with \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/compiler\/mcc.html \r\n% |-a|>. I'll discuss this point in a later post on \r\n% the\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/depfun.html\r\n% |depfun|> function.\r\n\r\n%% Your Arguments Don't Have to be Strings\r\n% I hope these examples demonstrate how to increase the sophistication of\r\n% your standalone executables' argument processing. Even though standalone\r\n% executables only receive strings from the command line, you can turn\r\n% those strings into many different kinds of data with a few extra lines of\r\n% MATLAB code. \r\n% \r\n% I leave you with exercise for the reader: how would you pass a MATLAB\r\n% structure to a standalone executable? I have a few ideas, but none of\r\n% them are really elegant. Post your suggestions\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=XXX258+more#respond here>.\r\n##### SOURCE END ##### e650b7cc174845fcb9ef3a86b041287f\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Guest blogger Peter Webb returns with another in an occasional series of postings about application deployment.\r\n      \r\n   \r\n   Contents\r\n   \r\n      \r\n   ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2011\/01\/06\/matlab-data-types-as-arguments-to-standalone-applications\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/260"}],"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=260"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/260\/revisions"}],"predecessor-version":[{"id":1946,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/260\/revisions\/1946"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}