{"id":35,"date":"2006-05-03T11:08:30","date_gmt":"2006-05-03T16:08:30","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=35"},"modified":"2018-01-08T16:16:35","modified_gmt":"2018-01-08T21:16:35","slug":"command-and-function-syntaxes-in-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2006\/05\/03\/command-and-function-syntaxes-in-matlab\/","title":{"rendered":"Command and Function Syntaxes in MATLAB"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>There is a fairly constant stream of questions in many MATLAB venues, including the <a>MATLAB newsgroup<\/a> that boil down to not understanding the difference between command and function syntaxes in MATLAB.  Today I try to set the\r\n         record straight.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Typical Question<\/a><\/li>\r\n         <li><a href=\"#3\">MATLAB Documentation<\/a><\/li>\r\n         <li><a href=\"#4\">MATLAB Function Syntax<\/a><\/li>\r\n         <li><a href=\"#10\">MATLAB Command Syntax<\/a><\/li>\r\n         <li><a href=\"#16\">Equivalent MATLAB Statements Using Command and Function Syntax<\/a><\/li>\r\n         <li><a href=\"#18\">How to Help Users Not Trip on Command vs. Function Syntax<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Typical Question<a name=\"1\"><\/a><\/h3>\r\n   <p>Why is MATLAB giving me the wrong answer here?<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">A = 1:4;\r\ndisp <span style=\"color: #A020F0\">A<\/span><\/pre><pre style=\"font-style:oblique\">A\r\n<\/pre><p>Why don't I see the values 1,...,4 found in variable <tt>A<\/tt>?\r\n   <\/p>\r\n   <h3>MATLAB Documentation<a name=\"3\"><\/a><\/h3>\r\n   <p>Before answering the user's question and discussing the details, I must admit that I think the documentation on MATLAB Calling Syntax is very clear on this topic.  Nonetheless, I will give my own take here.\r\n   <\/p>\r\n   <h3>MATLAB Function Syntax<a name=\"4\"><\/a><\/h3>\r\n   <p>We call MATLAB functions by listing their input arguments inside parentheses and separated by commas.  For example,<\/p>\r\n   <p><tt>sin(pi*(0:5)\/5)<\/tt><\/p>\r\n   <p>gives us a crudely sampled half sine wave.  To put that into a variable, use a variable name (or index into an existing variable)\r\n      followed by an equal sign (<tt>=<\/tt>) and then the expression being computed.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">hs = sin(pi*(0:5)\/5)<\/pre><pre style=\"font-style:oblique\">hs =\r\n         0    0.5878    0.9511    0.9511    0.5878    0.0000\r\n<\/pre><p>For multiple output variables, use a variables separated by commas and surrounded by square brackets for the output arguments:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[a,b,c] = deal(17)<\/pre><pre style=\"font-style:oblique\">a =\r\n    17\r\nb =\r\n    17\r\nc =\r\n    17\r\n<\/pre><p>Instead of explicit inputs, we can use variables and variable expressions.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">tpoints = (0:25)\/5;\r\nhalfsine = sin(pi*tpoints(1:6))<\/pre><pre style=\"font-style:oblique\">halfsine =\r\n         0    0.5878    0.9511    0.9511    0.5878    0.0000\r\n<\/pre><p>and if we wanted to compute this in single precision instead of using doubles, we can do something like this:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">tps = single(tpoints);\r\nhalfsines = sin(pi*tps(1:6))<\/pre><pre style=\"font-style:oblique\">halfsines =\r\n         0    0.5878    0.9511    0.9511    0.5878   -0.0000\r\n<\/pre><p>If we have a function that takes a string input, such as <tt>dir<\/tt>, we can either pass in the string explicitly:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dir(<span style=\"color: #A020F0\">'D:\\Loren\\BootCamp'<\/span>)<\/pre><pre style=\"font-style:oblique\">\r\n.           ..          fromDoug    fromStuart  \r\n\r\n<\/pre><p>or we can pass in a string variable.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dirname = <span style=\"color: #A020F0\">'D:\\Loren\\BootCamp'<\/span>\r\ndir(dirname)<\/pre><pre style=\"font-style:oblique\">dirname =\r\nD:\\Loren\\BootCamp\r\n\r\n.           ..          fromDoug    fromStuart  \r\n\r\n<\/pre><h3>MATLAB Command Syntax<a name=\"10\"><\/a><\/h3>\r\n   <p>Command syntax doesn't use parentheses, frequently doesn't place output in a variable, can't return multiple variables, and\r\n      handles <i>only<\/i> literal string inputs.  For example,\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">disp(dirname)<\/pre><pre style=\"font-style:oblique\">D:\\Loren\\BootCamp\r\n<\/pre><p>displays the string value of the variable <tt>dirname<\/tt>, while\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">disp <span style=\"color: #A020F0\">dirname<\/span><\/pre><pre style=\"font-style:oblique\">dirname\r\n<\/pre><p>displays the literal string <tt>dirname<\/tt>.\r\n   <\/p>\r\n   <p>Similarly, we can see the difference between the following 2 statements:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">disp <span style=\"color: #A020F0\">A<\/span>\r\ndisp(A)<\/pre><pre style=\"font-style:oblique\">A\r\n     1     2     3     4\r\n<\/pre><p>where the first statement displays the literal string <tt>A<\/tt> and the second statement displays the values that <tt>A<\/tt> contains.\r\n   <\/p>\r\n   <h3>Equivalent MATLAB Statements Using Command and Function Syntax<a name=\"16\"><\/a><\/h3>\r\n   <p>Here are some equivalent MATLAB statements for combining a string and a number to create a filename to pass to <tt>load<\/tt>.\r\n   <\/p>\r\n   <p><tt>load('file4')<\/tt><\/p>\r\n   <p><tt>load file4<\/tt><\/p>\r\n   <p><tt>fn = 'file4'; load(fn)<\/tt><\/p>\r\n   <p><tt>fn = 'file'; nfiles = 4; load([fn int2str(nfiles)])<\/tt><\/p>\r\n   <h3>How to Help Users Not Trip on Command vs. Function Syntax<a name=\"18\"><\/a><\/h3>\r\n   <p>Do you have any thoughts on how we can prevent users from tripping over command vs. function syntax in MATLAB?  If so, please\r\n      let me know.\r\n   <\/p>\r\n   <p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br>\r\n      Published with MATLAB&reg; 7.2<br><\/p>\r\n<\/div>\r\n<!--\r\n##### SOURCE BEGIN #####\r\n%% Command and Function Syntaxes in MATLAB\r\n% There is a fairly constant stream of questions in many MATLAB venues,\r\n% including the \r\n% <http:\/\/.shtml MATLAB newsgroup> \r\n% that boil down to not understanding the difference between command and\r\n% function syntaxes in MATLAB.  Today I try to set the record straight.\r\n\r\n%% Typical Question\r\n% Why is MATLAB giving me the wrong answer here?\r\n%\r\nA = 1:4;\r\ndisp A\r\n%%\r\n% Why don't I see the values 1,...,4 found in variable |A|?\r\n\r\n%% MATLAB Documentation\r\n% Before answering the user's question and discussing the details, I must\r\n% admit that I think the \r\n% <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/f7-59595.html#f7-58289 documentation>\r\n% on MATLAB Calling Syntax is very clear on this topic.  Nonetheless, I will give my own take here.\r\n%% MATLAB Function Syntax\r\n% We call MATLAB functions by listing their input arguments inside\r\n% parentheses and separated by commas.  For example,\r\n%\r\n% |sin(pi*(0:5)\/5)|\r\n%\r\n% gives us a crudely sampled half sine wave.  To put that into a variable,\r\n% use a variable name (or index into an existing variable) followed by an\r\n% equal sign (|=|) and then the expression being computed.\r\n%\r\nhs = sin(pi*(0:5)\/5)\r\n\r\n%%\r\n% For multiple output variables, use a variables separated by commas and\r\n% surrounded by square brackets for the output arguments:\r\n%\r\n[a,b,c] = deal(17)\r\n%%\r\n%\r\n% Instead of explicit inputs, we can use variables and variable\r\n% expressions.\r\n%\r\ntpoints = (0:25)\/5;\r\nhalfsine = sin(pi*tpoints(1:6))\r\n%%\r\n%\r\n% and if we wanted to compute this in single precision instead of using\r\n% doubles, we can do something like this:\r\n%\r\ntps = single(tpoints);\r\nhalfsines = sin(pi*tps(1:6))\r\n%%\r\n% If we have a function that takes a string input, such as |dir|, we can\r\n% either pass in the string explicitly:\r\ndir('D:\\Loren\\BootCamp')\r\n%%\r\n%\r\n% or we can pass in a string variable.\r\ndirname = 'D:\\Loren\\BootCamp'\r\ndir(dirname)\r\n%% MATLAB Command Syntax\r\n% Command syntax doesn't use parentheses, frequently doesn't place output\r\n% in a variable, can't return multiple variables, and handles _only_\r\n% literal string inputs.  For example,\r\ndisp(dirname)\r\n%%\r\n% displays the string value of the variable |dirname|, while\r\n\r\n%% \r\ndisp dirname\r\n%%\r\n% displays the literal string |dirname|. \r\n%% \r\n% Similarly, we can see the difference between the following 2\r\n% statements:\r\ndisp A\r\ndisp(A)\r\n%%\r\n% where the first statement displays the literal string |A| and the second\r\n% statement displays the values that |A| contains.\r\n%% Equivalent MATLAB Statements Using Command and Function Syntax\r\n% Here are some equivalent MATLAB statements for combining a string and a\r\n% number to create a filename to pass to |load|.\r\n%%\r\n%\r\n% |load('file4')|\r\n% \r\n% |load file4|\r\n% \r\n% |fn = 'file4'; load(fn)|\r\n% \r\n% |fn = 'file'; nfiles = 4; load([fn int2str(nfiles)])|\r\n%\r\n%% How to Help User's Not Trip on Command vs. Function Syntax\r\n% Do you have any thoughts on how we can prevent users from tripping over\r\n% command vs. function syntax in MATLAB?  If so, please\r\n% <http:?p=35\/Respond let me know>.\r\n\r\n\r\n##### SOURCE END #####\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      There is a fairly constant stream of questions in many MATLAB venues, including the MATLAB newsgroup that boil down to not understanding the difference between command and function... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/05\/03\/command-and-function-syntaxes-in-matlab\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,2],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/35"}],"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=35"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":2620,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/35\/revisions\/2620"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}