15 Feb 2017

Close all messageboxes in Matlab

收藏到CSDN网摘
In matlab, user interaction could be easily achieved by using 'msgbox()' function. The format of calling the 'msgbox()' is: msgbox(msg,title,style,mode). It is also very useful to modify the 'style' parameters to customise the appearance of the message box. The build-in parameters are:
'none' - the default one if you didn't provide any parameter
'warn' - warning dialogue box
'help' - help dialogue box
'error' - error dialogue box

The last parameter 'mode' can be selected among 'modal', 'nonmodal', and 'replace'. Some examples are shown in the picture below.


But, how to close these dialogue boxes? You might though: are you kidding me? click on the button - that's as easy as breath. Yes, that's exactly what everyone will do for sure. Seriously, think about if you have hundreds, even thousands of these little evils created in a loop? - yeah, I know it's a bad idea to create these GUI elements in your code if you are doing something computational extensive. But I saw someone did this before. It might be a legacy code you are maintaining which never come across this warning before. Or you just started from a small dataset and need to debug in this way while you progresses gradually. It's even worse if you created these dialogue boxes using 'modal' mode - it means you can do nothing before closing them. That's the real nightmare!

Let's try to find a way of dealing with this!

There are two ways of dealing with this problem using code rather than keyboard and mouse. It depends on how these boxes are created in the first place.

Method 1: you created these boxes by yourselves. It is easy! Do not ignore the return value while creating. Try to use 'handle = msgbox(...)'. Then you obtained the handle to that box you just created. To delete a specific box, you can use 'delete(handle)'. This is the standard way of proper coding. NEVER IGNORE RETURN VALUES! - no matter which programming languages you are using.

Method 2: sometimes, these dialogue boxes are created by other codes you used. You even do not have the source code of them. There is no way for you to delete them directly. Fortunately, Matlab was built on the idea of OOP. The objects are properly managed internally. For example, you can obtained the child objects of any valid objects using 'allchild(parent_handle)' function. The main Matlab session has the handle of 0. Some other useful object management functions are 'findobj()', 'findall()', 'get()' etc. Hold on, these are essential for us to do the job.

The idea is to: 1) find all handles of all objects in the current Matlab session; 2) get the title (or Tag) of them; 3) filter them to get only the msgbox handles; 4) delete the message boxes. Here is how:

handles = allchild(0);
tags = get(handles,'Tag');
isMsg = strncmp(tags,'Msgbox_',7); % all message boxes have the tags in the format of Msgbox_*
delete(handles(isMsg));

3 comments :

  1. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
    Pest Control in Chennai

    ReplyDelete
  2. Great post! I am actually getting ready to across this information, It's very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.

    Digital Marketing Company in Chennai

    ReplyDelete
  3. I am not sure the place you are getting your information, however good topic.I needs to spend some time studying more or understanding more.Thank you for wonderful information I was in search of this info for my mission.

    Staffing Companies in Chennai
    Recruitment Franchise in Chennai
    Staffing Companies in Bangalore

    ReplyDelete