{"id":14,"date":"2006-01-11T11:56:27","date_gmt":"2006-01-11T16:56:27","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=14"},"modified":"2018-01-16T10:34:24","modified_gmt":"2018-01-16T15:34:24","slug":"mental-model-for-feval","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2006\/01\/11\/mental-model-for-feval\/","title":{"rendered":"Mental Model for feval"},"content":{"rendered":"What's your mental model for when to use the MATLAB function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/feval.html\"><kbd>feval<\/kbd><\/a>?\r\n\r\n<p>\r\nUntil Release 14 (MATLAB 7), <kbd>feval<\/kbd> was <b><i>the<\/i><\/b> way to evaluate a function handle.   By that I mean you might write code something like this:\r\n<\/p>\r\n\r\n<pre class=\"code\">\r\nfunction y = halfcircle(fun,n)\r\nif nargin < 2\r\n    n = 20;\r\nend\r\ny = feval(fun,0:pi\/n:pi);\r\n<\/pre>\r\n\r\n<p>\r\nAnd you would call this function like this:\r\n<\/p>\r\n\r\n<pre class=\"code\">\r\nys = halfcircle(@sin);\r\nyc = halfcircle(@cos);\r\n<\/pre>\r\n<p>\r\n<kbd>feval<\/kbd> can also be used to evaluate functions when they are specified by their name. \r\n<\/p>\r\n\r\n<pre class=\"code\">\r\nfn = 'sin';\r\ny = feval(fn, 0:pi\/3:pi);\r\n<\/pre>\r\n<p>\r\nIt is more direct however to call the function itself instead of passing it to <kbd>feval<\/kbd>:\r\n<\/p>\r\n<pre class=\"code\">\r\ny = sin(0:pi\/3:3);\r\n<\/pre>\r\n<p>\r\nIn Release 14, we removed the need to use <kbd>feval<\/kbd> to evaluate function handles.  Instead, they can be evaluated directly.  Here's what the <kbd>halfcircle<\/kbd> code might look like in MATLAB 7.\r\n<\/p>\r\n<pre class=\"code\">\r\nfunction y = halfcircle(fh,n)\r\nif nargin < 2\r\n    n = 20;\r\nend\r\ny = fh(0:pi\/n:pi);\r\n<\/pre>\r\n<p>\r\nSo, I was curious recently when I saw some code posted in the MATLAB newsgroup containing a line that looked like this:\r\n<\/p>\r\n<pre class=\"code\">\r\nfiles = feval('dir','*.gif');\r\n<\/pre>\r\n<p>\r\nThis was interspersed with code containing calls to <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/length.html\"><kbd>length<\/kbd><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/imread.html\"><kbd>imread<\/kbd><\/a>, and several other functions, all of which were called directly.\r\n<\/p>\r\n<p>\r\nDoes anyone have insight about what distinction a user might have made between the MATLAB function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/dir.html\"><kbd>dir<\/kbd><\/a> and other ones?\r\n<\/p>\r\n<p>\r\nPlease post your comments and thoughts.  I'd love to hear how you think about this.\r\n<\/p>","protected":false},"excerpt":{"rendered":"<p>What's your mental model for when to use the MATLAB function feval?\r\n\r\n\r\nUntil Release 14 (MATLAB 7), feval was the way to evaluate a function handle.   By that I mean you might write code something... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/01\/11\/mental-model-for-feval\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,8],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/14"}],"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=14"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":2646,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/14\/revisions\/2646"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}