{"id":50,"date":"2006-08-16T14:00:10","date_gmt":"2006-08-16T19:00:10","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=50"},"modified":"2017-08-28T20:12:04","modified_gmt":"2017-08-29T01:12:04","slug":"handle-this","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2006\/08\/16\/handle-this\/","title":{"rendered":"Handle This!"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I recently got a great request from a reader to discuss the various kinds of entities in MATLAB attached to the word <i>handle<\/i>. I want to talk about the parallels and differences of three affecting most MATLAB users:\r\n      <\/p>\r\n      <div>\r\n         <ul>\r\n            <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fopen.html\">file identifiers<\/a><\/li>\r\n            <li><a title=\"https:\/\/www.mathworks.com\/help\/matlab\/learn_matlab\/understanding-handle-graphics-objects.html (link no longer works)\">handle graphics objects<\/a><\/li>\r\n            <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/function-handles.html\">function handles<\/a><\/li>\r\n         <\/ul>\r\n      <\/div>\r\n      <p>And, of course, there's also <a href=\"http:\/\/en.wikipedia.org\/wiki\/Handel\">George Frideric Handel<\/a><\/p>\r\n      <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/50\/handel.png\"> <\/p>\r\n      <p>To hear more, try this in MATLAB:<\/p><pre>               load handel\r\n               sound(y,Fs)<\/pre><\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Similarities<\/a><\/li>\r\n         <li><a href=\"#2\">Differences<\/a><\/li>\r\n         <li><a href=\"#15\">Comparisons<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Similarities<a name=\"1\"><\/a><\/h3>\r\n   <p>In all three cases, the handle or identifier let's you refer to another entity and possibly change its state, without creating\r\n      a new variant.\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>For files, this means that I can read part of a file and advance through it while always being able to refer to that particular\r\n            file.\r\n         <\/li>\r\n         <li>Similarly, for graphics handles, I can change properties associated with the objects being referred, e.g., their colors, without\r\n            changing how I refer to the graphics object itself.\r\n         <\/li>\r\n         <li>Finally, for function handles, I can refer to a particular function via its handle even when conditions, such as the path\r\n            or current directory, change.  Also, for handles to nested functions, I am able to change the state of the function handle's\r\n            workspace without needing to create a new way to refer to that particular instance of a function.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Differences<a name=\"2\"><\/a><\/h3>\r\n   <div>\r\n      <ul>\r\n         <li>File handles and handle graphics objects all have top-level ways to locate them, even if they aren't assigned a variable in\r\n            your current workspace.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Let me start by exploring a little bit about files.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">fid1 = fopen(<span style=\"color: #A020F0\">'cumtrapz.m'<\/span>);\r\nfid2 = fopen(<span style=\"color: #A020F0\">'trapz.m'<\/span>);\r\nfid3 = fopen(<span style=\"color: #A020F0\">'mean.m'<\/span>);\r\nclear<\/pre><p>Now find the open files even though the workspace is empty.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">fids = fopen(<span style=\"color: #A020F0\">'all'<\/span>)<\/pre><pre style=\"font-style:oblique\">fids =\r\n     3     4     5\r\n<\/pre><p>I have to close the files one at a time.  Successful closure returns a 0. Make sure they <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/all.html\"><tt>all<\/tt><\/a> succeed.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">success = 0;\r\n<span style=\"color: #0000FF\">if<\/span> all(arrayfun(@fclose, fids)==0)\r\n    disp(<span style=\"color: #A020F0\">'Success, all files closed'<\/span>)\r\n<span style=\"color: #0000FF\">else<\/span>\r\n    disp <span style=\"color: #A020F0\">Oops!<\/span>\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">Success, all files closed\r\n<\/pre><p>Prove that no files are open.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">fids = fopen(<span style=\"color: #A020F0\">'all'<\/span>)<\/pre><pre style=\"font-style:oblique\">fids =\r\n     []\r\n<\/pre><p>I can also use probe and manipulate within an open file using <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fseek.html\"><tt>fseek<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/ftell.html\"><tt>ftell<\/tt><\/a> to change and query the position indicator in the file.\r\n   <\/p>\r\n   <p>Clear the workspace again, and any possibly lurking figures.<\/p>\r\n   <p>Next I will explore more about graphics handles by creating a plot and finding the line contained in the figure.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">clear <span style=\"color: #A020F0\">all<\/span>\r\nclose <span style=\"color: #A020F0\">all<\/span>\r\nload <span style=\"color: #A020F0\">handel<\/span>\r\nt = (0:(numel(y)-1))\/Fs;\r\nplot(t,y)\r\ntitle <span style=\"color: #A020F0\">Handel<\/span>\r\nxlabel <span style=\"color: #A020F0\">Seconds<\/span>\r\nclear<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/50\/handleThis_01.png\"> <p>Now let me find the line I've drawn.  I'll start by getting the children of the root from handle value 0.  Then I'll get the\r\n      axes, and finally the line inside.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">figs = get(0,<span style=\"color: #A020F0\">'Children'<\/span>)\r\nax = get(figs, <span style=\"color: #A020F0\">'Children'<\/span>)\r\nhandelLine = get(ax,<span style=\"color: #A020F0\">'Children'<\/span>)<\/pre><pre style=\"font-style:oblique\">figs =\r\n     1\r\nax =\r\n  160.0015\r\nhandelLine =\r\n  161.0034\r\n<\/pre><p>To prove it's the same line, let me change the color.  Notice that the handle itself has not changed value but the color property\r\n      associated with it did.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">set(handelLine,<span style=\"color: #A020F0\">'Color'<\/span>,<span style=\"color: #A020F0\">'g'<\/span>)\r\nhandelLine<\/pre><pre style=\"font-style:oblique\">handelLine =\r\n  161.0034\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/50\/handleThis_02.png\"> <p>For function handles, I get use the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/functions.html\"><tt>functions<\/tt><\/a> function to query the state, possibly contained, for example, in a nested workspace.  I cannot, however, directly change\r\n      the state.  I must use the function handle itself or some other function handle that has access to the nested workspace in\r\n      order to affect a change.\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>File handles and handle graphics objects all have ways to dispose of them.  For files, I simply use <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fclose.html\"><tt>fclose<\/tt><\/a>, and for graphics objects, I <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/delete.html\"><tt>delete<\/tt><\/a> them.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>For function handles, I must delete every possible variable and callback that points to the handle in question to be sure\r\n      that everything associated with the function handle has been purged.\r\n   <\/p>\r\n   <h3>Comparisons<a name=\"15\"><\/a><\/h3>\r\n   <p>File identifiers, handles to graphics objects, and function handles all use similar nomenclature and they share a lot of functionality\r\n      and interaction in common.  File identifiers and graphics objects can have persistence beyond the lifetime of the variables\r\n      and programs that create them, whereas function handles last only until the last reference to them disappears.  Please add\r\n      <a href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/08\/16\/handle-this\/#respond\">your thoughts<\/a> to these comparisons.\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%% Handle This!\r\n% I recently got a great request from a reader to discuss the various kinds\r\n% of entities in MATLAB attached to the word _handle_.\r\n% I want to talk about the parallels and differences of three affecting\r\n% most MATLAB users:\r\n% \r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/fopen.html file identifiers> \r\n% * <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/creating_plots\/hg_objec.html handle graphics objects> \r\n% * <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/ref\/function_handle.html function handles>\r\n%\r\n% And, of course, there's also <http:\/\/en.wikipedia.org\/wiki\/Handel George Frideric Handel>\r\n%\r\n% <<handel.png>>\r\n%\r\n% To hear more, try this in MATLAB: \r\n%\r\n%                 load handel\r\n%                 sound(y,Fs)\r\n%\r\n\r\n%% Similarities\r\n% In all three cases, the handle or identifier let's you refer to another\r\n% entity and possibly change its state, without creating a new variant.\r\n%\r\n% * For files, this means that I can read part of a file and advance through\r\n% it while always being able to refer to that particular file.  \r\n% * Similarly,\r\n% for graphics handles, I can change properties associated with the\r\n% objects being referred, e.g., their colors, without changing how I refer\r\n% to the graphics object itself.  \r\n% * Finally, for function handles, I can\r\n% refer to a particular function via its handle even when conditions, such\r\n% as the path or current directory, change.  Also, for handles to nested\r\n% functions, I am able to change the state of the function handle's\r\n% workspace without needing to create a new way to refer to that particular\r\n% instance of a function.\r\n%\r\n%% Differences\r\n% \r\n% * File handles and handle graphics objects all have top-level ways to\r\n% locate them, even if they aren't assigned a variable in your current\r\n% workspace.\r\n%\r\n%%\r\n% Let me start by exploring a little bit about files.\r\nfid1 = fopen('cumtrapz.m');\r\nfid2 = fopen('trapz.m');\r\nfid3 = fopen('mean.m');\r\nclear\r\n%% \r\n% Now find the open files even though the workspace is empty.\r\nfids = fopen('all')\r\n%%\r\n% I have to close the files one at a time.  Successful closure returns a 0.\r\n% Make sure they\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/all.html |all|> succeed.\r\nsuccess = 0;\r\nif all(arrayfun(@fclose, fids)==0)\r\n    disp('Success, all files closed')\r\nelse \r\n    disp Oops!\r\nend\r\n%%\r\n% Prove that no files are open.\r\nfids = fopen('all')\r\n%%\r\n% I can also use probe and manipulate within an open file using\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/fseek.html |fseek|>\r\n% and <https:\/\/www.mathworks.com\/help\/matlab\/ref\/ftell.html |ftell|>\r\n% to change and query the position indicator in the file.\r\n%%\r\n% Clear the workspace again, and any possibly lurking figures.  \r\n%%\r\n% Next I will explore more about graphics handles by creating\r\n% a plot and finding the line contained in the figure.\r\nclear all\r\nclose all\r\nload handel\r\nt = (0:(numel(y)-1))\/Fs;\r\nplot(t,y)\r\ntitle Handel \r\nxlabel Seconds\r\nclear\r\n%%\r\n% Now let me find the line I've drawn.  I'll start by getting the children\r\n% of the root from handle value 0.  Then I'll get the axes, and finally\r\n% the line inside.\r\nfigs = get(0,'Children')\r\nax = get(figs, 'Children')\r\nhandelLine = get(ax,'Children')\r\n%%\r\n% To prove it's the same line, let me change the color.  Notice that the\r\n% handle itself has not changed value but the color property associated\r\n% with it did.\r\nset(handelLine,'Color','g')\r\nhandelLine\r\n%%\r\n% For function handles, I get use the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/functions.html |functions|>\r\n% function to query the state, possibly contained, for example, in a nested\r\n% workspace.  I cannot, however, directly change the state.  I must use the\r\n% function handle itself or some other function handle that has access to\r\n% the nested workspace in order to affect a change.\r\n%%\r\n% * File handles and handle graphics objects all have ways to dispose\r\n% of them.  For files, I simply use \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/fclose.html |fclose|>,\r\n% and for graphics objects, I\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/delete.html |delete|> \r\n% them.\r\n%%\r\n% For function handles, I must delete every possible variable and callback\r\n% that points to the handle in question to be sure that everything\r\n% associated with the function handle has been purged.\r\n%% Comparisons\r\n% File identifiers, handles to graphics objects, and function handles\r\n% all use similar nomenclature and they share a lot of functionality and\r\n% interaction in common.  File identifiers and graphics objects can have\r\n% persistence beyond the lifetime of the variables and programs that create\r\n% them, whereas function handles last only until the last reference to them\r\n% disappears.  Please add <?p=50#respond your thoughts> to these\r\n% comparisons.\r\n\r\n##### SOURCE END #####\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I recently got a great request from a reader to discuss the various kinds of entities in MATLAB attached to the word handle. I want to talk about the parallels and differences of three... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/08\/16\/handle-this\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,3,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/50"}],"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=50"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"predecessor-version":[{"id":2405,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/50\/revisions\/2405"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}