I'm writing an application in MATLAB and want to update the look of it. is it possible to change the icons of the buttons in the toolbar in MATLAB code?
The code will be compiled and I am not using GUIDE; ideally there's a way to get the button handles and set each icon individually although I don't know how I would do this.
But with higher quality icons.
Yes, you can change the figure toolbar icons, or add your own.
I've detailed how to change the icon below, as well as other useful things to do with the toolbar whilst you're editing the properties anyway.
See the code comments for details.
Get the toolbar object
% Initialise some figure
fig = figure( 'Name', 'MyApp', 'NumberTitle', 'off' )
% Get the figure toolbar handle
tbar = findall( fig, 'tag', 'FigureToolBar' );
You can do findall(tbar) at this point to list the names of all the buttons
Hiding buttons
Let's say you want to hide the "new figure" button:
% Get the button from the tbar object
btn = findall( tbar, 'tag', 'Standard.NewFigure' )
% Set to not visible
set( btn, 'Visible', 'off' );
Changing callbacks
Let's say you want the print button to trigger a print-preview callback instead of printing directly (you could assign any custom callback function to any button):
% Get the button again
btn = findall( tbar, 'tag', 'Standard.PrintFigure' );
% Change the callback (and the tooltip to match)
set( btn, 'ClickedCallback', 'printpreview(gcbf)', ...
'TooltipString', 'Print preview' );
Changing the icon
At this point you can see all button attributes are editable, including the image as per your original question.
In particular, just change the CData property:
% Update the print button to have a print preview icon
% This should be a 16*16 RBG image matrix, use imread to get from file
img = imread( 'printpreview.bmp' )
% Assign CData property to button already acquired
set( btn, 'CData', img );
Output (I used a random print preview icon, seen 4th from the left):
Add new buttons
You can add new buttons by simply creating uipushtool objects (with CData property set for the icon image) with the tbar object as a parent.
Change the separators
The vertical grey separators can be added or removed (useful for creating your own button groups or when removing buttons). Simply set the 'Separator' property to 'off' or 'on', for a separator on the left side of a button.
For a compiled app, this may be against The MathWorks T&Cs, but this is the how to not the should you do this!
Related
I made a GUI with two uipanels, each containing 4 image axes.
I plotted different figures on each panel and want to switch between panels by a push button.
For this I used the following:
Initially, I set uipanel2 to 'visible' 'on' and uipanel3 to 'visible' 'off';
Then when I push 'push button' it checks if uipanel is 'on' and turns on and off respectively.
% Code:
set(handles.uipanel2,'visible','on');
set(handles.uipanel3,'visible','off');
% When I push 'push button':
if strcmp(get(handles.uipanel2,'visible'),'on')
disp('panel-2 onn switching it off')
set(handles.uipanel2,'visible','off');
set(handles.uipanel3,'visible','on');
elseif strcmp(get(handles.uipanel3,'visible'),'on')
disp('panel-3 onn switching it off')
set(handles.uipanel3,'visible','off');
set(handles.uipanel2,'visible','on');
end
It doesn't work as expected I don't see panels switching.
For displaying images I used code like this:
% Panel-2
axes(handles.RCC);
imshow(img_RCC,lims);
axes(handles.LCC);
imshow(img_LCC,lims);
axes(handles.RML);
imshow(img_RML,lims);
axes(handles.LML);
imshow(img_LML,lims);
% Panel-3
axes(handles.RCC_Orig);
imshow(img_RCC,lims);
axes(handles.LCC_Orig);
imshow(img_LCC,lims);
axes(handles.RML_Orig);
imshow(img_RML,lims);
axes(handles.LML_Orig);
imshow(img_LML,lims);
Update: I can only see the GUI panel on top being 'not visible' and switching to 'visible'. The panel on bottom I think is still there but I don't know how to make it come on top
I have a Matlab GUIDE figure with nested panels. I also have control objects on top of the panels. Here's a notional example:
In order to align all of the checkboxes, they all need to be in the same control group. I want to move the checkboxes so that their parent is the main panel rather than one of the sub-panels. The sub-panels are just there for visual grouping and don't really have a functional value.
The object browser shows the relationships, but I see no way to change it. I've tried pasting the objects I want to move outside the sub-panel and dragging them back in, but they automatically get added to the sub-panel. If I paste outside of the sub-panel I can use the arrow keys to move them back in and the parent will stay the main panel, but that gets to be tedious.
How can I change the parent panel of a GUIDE control object?
Building the GUI with guide a possible solution could be:
add the main uipanel
add all the checkbox in the main `uipanel'
align them (e. g. by selecting some of them and then use the Align Objects tool on the guide toolbar
add the secondary uipanel over some of the checkbox. It will cover the `checkbox'
select the secondary uipanel
right-click on the mouse to pop-up the context menu
select the Send to Back option
If you want to create the GUI "programmatically" you can:
create a figure
add the main panel with uipanel
add the checkboxes with uicontrol setting the Style property to checkbox and the position property so that they will be placed in the main panel
add the secondary uipanel. Also in this case the secondary uipanel will mask the checkbox
push the secondary panel back using the uistack function
Thsi is a possible implementation
% Create the figure
figure
% Add the Main uipanel
p1=uipanel('units','normalized','position',[.1 .1 .5 .7],'title','Main Panel')
% Add come checkboxes
cb1=uicontrol('parent',p1,'style','checkbox','units','normalized', ...
'position',[.1 .7 .3 .1],'string','checkbox 1')
cb2=uicontrol('parent',p1,'style','checkbox','units','normalized', ...
'position',[.1 .6 .3 .1],'string','checkbox 2')
cb3=uicontrol('parent',p1,'style','checkbox','units','normalized', ...
'position',[.1 .5 .3 .1],'string','checkbox 3')
% Add the Secondary uipanel
p2=uipanel('parent',p1,'units','normalized','position',[.05 .4 .4 .5], ...
'title','Secondary Panel')
% Push the secondary uipanel back
uistack(p2,'bottom')
Hope this helps.
Qapla'
I have 6 uipanels ,all same size overlapped one over another. I have to add buttons edit text to all the uipanels.but I can edit only 6th uipanel and all others are hidden.how can I make only one uipanel visible at a time so that I can easily add buttons and text to it. I have to add a push button in each panel which when clicked should show succeeding uipanel and hide previous uipanel.
For example,
I have a uipanel1 with a push button.when. I click the push button,it should show uipanel2 and hide uipanel1.
thanks
First of all you have to define yours uipanels as variables. For example, uipanel1 = uipanel(...); uipanel2 = uipanel(...)
So you can access easily to your six uipanel handles and put these handles in your button callback function.
Another solution would be to use the 'Tag' property for the uipanel in order to identify the uipanel. It may be longer but by using the findobj('Tag','uipanel1_tag') function, you can easily find the desired handle.
I know that one can insert a colorbar by clicking the colorbar icon in the clustergram GUI. Is there a way to do it programmatically?
I tried
cgo = clustergram(data)
colorbar;
This makes a colorbar in a new figure window. How can a colorbar be created with proper positioning in a clustergram figure as if the button was clicked?
There is a function buried away (HeatMap.plot>showColorbar) that neatly positions the colorbar to the left of both the heat map and the dendogram (the lines). Just running colorbar(...) will mess up the relative positioning of the dendogram and the heatmap. So you need to somehow run the callback or carefully duplicate all of the position computations. It's easier to just run the callback. Here's how.
To create the colorbar programmatically for a clustergram, and keep the color bar button in sync, you need to use the button's assigned callback and set the button's state.
Create the clustergram:
load filteredyeastdata
cgo = clustergram(yeastvalues(1:30,:),'Standardize','Row');
Get the handle for color bar button:
cbButton = findall(gcf,'tag','HMInsertColorbar');
Get callback (ClickedCallback) for the button:
ccb = get(cbButton,'ClickedCallback')
ccb =
#insertColorbarCB
[1x1 clustergram]
That gives us a handle to the function assigned by the callback (#insertColorbarCB), and the function's third input argument (the clustergram object). The button's handle and an empty event object are implicitly the first two arguments.
Change the button state to 'on' (clicked down):
set(cbButton,'State','on')
Run the callback to create the colorbar:
ccb{1}(cbButton,[],ccb{2})
Note that the button State must be changed to 'on' first, otherwise the callback won't do anything.
I just managed to solve this problem.
What I did:
I added this function to the clustergram code (I put it at line 1486)
%%%%%%%%%%%%%%
function insertColorbarCBALWAYS(obj)
hFig= gcbf;
obj.Colorbar = true;
end
%%%%%%%%%%%%%%%
and then at line 415 of the clustergram.m file I added this line of code
insertColorbarCBALWAYS(obj);
to call the above function. Save and go: now the colorbar will always be there, once the clustergram is drawn.
Previous method was not working for me so I made this workaround.
One may even save the new clustergram code as clustergramCM such that you can draw cgram in both ways.
I want to have an "edit" box in a MATLAB GUI that says "TYPE SEARCH HERE".
When the user clicks inside the box I want the "TYPE SEARCH HERE" to disappear and give the user an empty edit box to start typing in...
Any ideas?
At least on my system, when I use the follow code to set up a user input box/window
prompt = 'Enter search terms:';
dlg_title = 'My input box';
num_lines = 1;
defAns = {'TYPE_SERACH_HERE'};
answer = inputdlg(prompt, dlg_title, num_lines, defAns);
the default text TYPE_SEARCH_HERE appears highlighted, so I can just start typing to replace it with what ever I want.
Edit Alternatively, if you have an existing uicontrol edit box you could do something like the following:
function hedit = drawbox()
hedit = uicontrol('Style', 'edit',...
'String', 'deafult',...
'Enable', 'inactive',...
'Callback', #print_string,...
'ButtonDownFcn', #clear);
end
function clear(hObj, event) %#ok<INUSD>
set(hObj, 'String', '', 'Enable', 'on');
uicontrol(hObj); % This activates the edit box and
% places the cursor in the box,
% ready for user input.
end
function print_string(hObj, event) %#ok<INUSD>
get(hObj, 'String')
end
Chris, you've got to click in the uicontrol border to make the ButtonDownFcn happen. It won't happen if you click inside the edit box
Okay, so I have a solution to the problem and it works flawlessly!!
However, I am quite upset because I have absolutely no idea WHY it works...
Create an edit text box in GUIDE and right click on it to open up property inspector.
add text "TYPE TEXT HERE" to "string" property
Find the property nammed "Enable" and switch it to "inactive"
Create a buttonDownFnc (can be done in property inspector also)
Use following code:
function myEditBoxTagGoesHere_ButtonDownFcn(hObject, eventdata, handles)
% Toggel the "Enable" state to ON
set(hObject, 'Enable', 'On');
% Create UI control
uicontrol(handles.myEditBoxTagGoesHere);
If someone could explain why uicontrol highlights the text upon left mouse click, that would be great!
Hooplator15, it works because edit texts are like push buttons when Enable is Off:
If Enable == 'on' (edit text Enable), the function _ButtonDownFcn executes on mouse press in 5 pixel border;
Otherwise, it executes on mouse press in 5 pixel border or over edit text, like a button.