{"id":16,"date":"2006-02-01T11:51:26","date_gmt":"2006-02-01T16:51:26","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=16"},"modified":"2006-01-24T10:26:50","modified_gmt":"2006-01-24T15:26:50","slug":"existence-in-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2006\/02\/01\/existence-in-matlab\/","title":{"rendered":"Existence in MATLAB"},"content":{"rendered":"Users have needed access to information about the existence of variables, files, etc. in MATLAB for a long time.  The function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/exist.html\"><kbd>exist<\/kbd><\/a> allows us to programmatically check for these entities.\r\n\r\n<p>\r\n<kbd>exist<\/kbd> can be called with one argument (a string) to test if that specific name is known and available in MATLAB in any form.  If we program carefully and check the output result of <kbd>exist<\/kbd>, then we can be sure we know whether we are dealing with a MATLAB variable, a Simulink model, etc.\r\n<\/p>\r\n<p>\r\nI often see user's calling <kbd>exist<\/kbd> with a single input argument, a name.   This  answers whether or not that name is somehow in the MATLAB environment.  Very often, however, we really want more specific information, such as whether or not that name refers to a variable.  A careful check of the output value (not just >0) can supply this information, though I rarely see code check for this.\r\n<\/p>\r\n<p>\r\nUsing the two input syntax for <kbd>exist<\/kbd>, we are able to write code that is both more robust, more readable, and frequently more efficient.\r\n<\/p>\r\n<p>\r\nFor example, if we  have a program in which we ask the user to type in a variable name to process, we may then want to verify that the variable exists or else prompt the user again for a valid variable name.  To do so, we might write code like this:\r\n<\/p>\r\n<pre class=\"code\">\r\nvarname = '';\r\nwhile isempty(varname)\r\n     varname = input('Which variable would you like to process?','s');\r\n     if ~exist(varname,'var')\r\n         varname = '';\r\n     end\r\nend\r\n<\/pre>\r\n<p>\r\nWhen we run this code, since we are only checking for variables, MATLAB does not need to go look on the file system for all the other possible items MATLAB might be interested in such as M-Files, etc.  This can result in a tremendous speed gain!  And, by confining the search to variables, the code is more robust since it can't get misled if the name refers to an M-File.\r\n<\/p>","protected":false},"excerpt":{"rendered":"<p>Users have needed access to information about the existence of variables, files, etc. in MATLAB for a long time.  The function exist allows us to programmatically check for these entities.\r\n\r\n\r\nexist... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/02\/01\/existence-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":[10,11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/16"}],"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=16"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}