3

I am trying to build a GUI with several tabs, using uitabpanel() found at http://www.mathworks.com/matlabcentral/fileexchange/11546 . I would like to resize the GUI based on the currently opened tab; this is available with uitabpanel.SelectedItem.

Basically I would like to build a callback in order to monitor uitabpanel.SelectedItem - when this variable changes, the GUI window should resize appropriately. Generally speaking, I am looking for a way to monitor a variable and execute a callback when the variable changes value.

Is this possible? How would I go about doing this?

Thanks!

2 Answers 2

4

I don't have a MATLAB in front of me right now, but if it is implemented as a property -- and as far as I can tell from a quick look at the code, it is -- you can use addlistener function and provide a callback function for it.

addlistener(hTab,'SelectedItem','PostSet',@(s,e)disp('SelectedItem changed'))
Sign up to request clarification or add additional context in comments.

1 Comment

That is EXACTLY what I needed! It turns out that SelectedItem is not a property, but the underlying tab panels are just uipanel objects, and I can use what you propose by watching the Visible property for each tab. Thanks!!!
3

I'm not familiar with uitab from the file exchange. However, if it's build upon the built-in uitab, then there should be a selectionChangeCallback or selectionChangeFcn property (depending on your Matlab version). Specify a function for this callback property, and you have a way to execute a function whenever the selection changes.

If that's not possible, the only other way to monitor a variable change (if you can't somehow use objects and set methods) is to use a TIMER OBJECT that periodically polls the value of the variable.

EDIT Since the FEX uitab is based on uipanel, the callback you're looking for is most likely ButtonDownFcn. Before you change it, make sure that it's not used by the uitab function, otherwise, you will want to edit that function.

5 Comments

I could definitely use OOP for this, but I feel like it's kind of like sandblasting a soup cracker - overkill for what I thought should be a fairly simple task. I guess it's not as simple as I thought. I don't think uitabpanel() is based on uitab(), but I will definitely try out the callbacks you said. I don't think the former is based on the latter. Thanks a lot!
Looks like uitabpanel() is based on uipanel(). I suppose I could switch to uitab(), but frankly uitabpanel() looks much prettier..
@strictlyrude27: Ah, ok, You may want to have a look at the ButtonDownFcn then, which is executed every time you click on the uipanel.
Thanks Jonas - I ended up trying that out. ButtonDownFcn sadly only works when you click inside the panel - clicking the tab itself doesn't execute the callback. Perhaps going to uitab is the best bet? I don't like the timer object idea since I don't want to actively poll the variable.
There is a way to monitor a variable change, if it is a property, by using the addlistener function. See my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.