How do I create a tabbed GUI in MatLab? - matlab

I want to create a tabbed GUI in which first tab is for reading input, then the input is displayed on the GUI. The User should be able to select the data from GUI and then given as input to a algorithm. Also the user can select parameters for the algortihm in another tab. In the third tab, the user can see the resulting plots.
How do I create a tabbed GUI within MatLab either programmatically or using the GUIDE?

Here is a simple example using the semi-documented function UITAB to create tabs:
function tabbedGUI()
%# create tabbed GUI
hFig = figure('Menubar','none');
s = warning('off', 'MATLAB:uitabgroup:OldVersion');
hTabGroup = uitabgroup('Parent',hFig);
warning(s);
hTabs(1) = uitab('Parent',hTabGroup, 'Title','Data');
hTabs(2) = uitab('Parent',hTabGroup, 'Title','Params');
hTabs(3) = uitab('Parent',hTabGroup, 'Title','Plot');
set(hTabGroup, 'SelectedTab',hTabs(1));
%# populate tabs with UI components
uicontrol('Style','pushbutton', 'String','Load data...', ...
'Parent',hTabs(1), 'Callback',#loadButtonCallback);
uicontrol('Style','popupmenu', 'String','r|g|b', ...
'Parent',hTabs(2), 'Callback',#popupCallback);
hAx = axes('Parent',hTabs(3));
hLine = plot(NaN, NaN, 'Parent',hAx, 'Color','r');
%# button callback
function loadButtonCallback(src,evt)
%# load data
[fName,pName] = uigetfile('*.mat', 'Load data');
if pName == 0, return; end
data = load(fullfile(pName,fName), '-mat', 'X');
%# plot
set(hLine, 'XData',data.X(:,1), 'YData',data.X(:,2));
%# swithc to plot tab
set(hTabGroup, 'SelectedTab',hTabs(3));
drawnow
end
%# drop-down menu callback
function popupCallback(src,evt)
%# update plot color
val = get(src,'Value');
clr = {'r' 'g' 'b'};
set(hLine, 'Color',clr{val})
%# swithc to plot tab
set(hTabGroup, 'SelectedTab',hTabs(3));
drawnow
end
end

You can also create tabs from a GUIDE created GUI with the help of a utility available from Matlab File Exchange that I wrote.
The usage is fairly simple:
Create a pane with tag set to Tab? where ? is any letter or number (e.g. TabA). This main pane should be left empty and determines the size and location of the tab group (uitabgroup).
Create additional panes with a tag name that starts with the name of the main pane. All other controls should be added to these panes.
In the Guide generated function xxx_OpeningFcn add the following:
handles.tabManager = TabManager( hObject );
The location of the additional panes is not important but it is generally easier to edit the GUI if they are in the same location as the main pane. You can edit the panes even if they are overlaid by cycling through the panes with the "Send to back" command from the Guide pop up menu.

Related

Right-align values in UITable in App Designer

I already asked about this on Matlab Answers but did not get a response there, so I try it here.
I have some numbers I want to show in a UITable. Since I need a specific formatting of the values (three digits after comma, no scientific notation), I converted the values to strings. The problem is that these strings are by default left-aligned which looks not very nice.
When using UITable in GUIDE, I was able to pad strings with leading spaces to get them right aligned like in the following
data = {'1532.000'; ' 5.543'; ' 26.457'};
Using a mono space font, the values are shown like this in the table:
1532.000
5.543
26.457
Currently I am considering switching to App Designer. I am using the same space-padded strings but here the uitable seems to strip them off. That is the result looks like the following:
1532.000
5.543
26.457
Is there a way to make uitable in App Designer keep the spaces like it did in GUIDE?
Of course it would be even better to directly right-align the strings without the need of padding, but as far as I know this is not possible.
In case it matters: I am using Matlab R2016b.
Edit:
Minimal example for generating and filling a UITable. This is a simple AppDesigner GUI where I added only a table and a button (without modifying any attributes). The click callback of the button is used to add the data to the table.
classdef test < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
UITable matlab.ui.control.Table
Button matlab.ui.control.Button
end
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
data = {'1532.000'; ' 5.543'; ' 26.457'};
app.UITable.Data = data;
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
setAutoResize(app, app.UIFigure, true)
% Create UITable
app.UITable = uitable(app.UIFigure);
app.UITable.ColumnName = {'Column 1'; 'Column 2'; 'Column 3'; 'Column 4'};
app.UITable.RowName = {};
app.UITable.Position = [127 180 302 185];
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, #ButtonPushed, true);
app.Button.Position = [220 104 100 22];
end
end
methods (Access = public)
% Construct app
function app = test()
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
After pushing the button the table looks as follows:
Note that AppDesigner only allows me to modify the code inside the ButtonPushed function.
After investigating this for a while, I found that the reason for the "special" behavior when it comes to a uitable is because it's actually a Java object! (I have no idea how they managed to pull this off or why). In any case, right-aligning the data can be done by inserting one line to your script:
data = {'1532.000'; ' 5.543'; ' 26.457'};
app.UITable.Data = data;
setColumnFormat(java(app.UITable),{'numeric'}); % < Add this
if there's more than one column that you would like to right-align this way, simply replicate {'numeric'} as many times as needed:
setColumnFormat(java(app.UITable), repmat( {'numeric'}, 1, size(data,2) ) );
There's also a slightly shorter notation possible for the styling commands above (thanks #luator for pointing this out):
app.UITable.ColumnFormat = {'numeric'};
app.UITable.ColumnFormat = repmat( {'numeric'}, 1, size(data,2) );
The result:
If I understood Yair's blog correctly, you can find more information about the customization of these table objects by looking for "JIDE Table", or SortableTable.

Matlab: display crosshairs simultaneously on two images in GUI

I'm building a MATLAB GUI using GUIDE for processing medical images (brain tumor segmentation on MRI scans). As a preprocessing step, the program coregisters different scans, which looks like this:
I now want to display crosshairs on both images as a visual check of the coregistration. The crosshairs should be linked to one another, such that they point to the same pixel in both images. Moreover, it should move when hovering over (or clicking on) one of the images. This is what I want to achieve:
Does a build-in MATLAB function exist that can achieve this? Or, if I have to write it myself, how would one tackle this problem?
I'll use a Toy GUI in order to show how it works :
Let's first try to make it work for the first image only. What you want to achieve is :
When the user clicks on the figure, we want to have access to the location of the mouse click.
Then we want to check if the click was located inside the first image
If it is, we want to update the position of the crossbar, which will be represented by two lines overlayed on the image and crossing at the selected point.
Step 0 : Initial settings
You want to make sure of some details first :
Make sure you add a call to hold on after both your imshow calls, otherwise the crossbars will delete your image instead of overlaying on it.
Step 1 : Getting the position of the mouse click
In your GUI opening function (here it will be SuperDuperGUI_OpeningFcn), you want to add a call to :
set(gcf, 'WindowButtonDownFcn', #getMousePositionOnImage);
This will trigger the function getMousePositionOnImage everytime the user clicks inside your GUI.
Then, you want to add and implement the function getMousePositionOnImage :
function getMousePositionOnImage(src, event)
% Fetch the current handles structure
handles = guidata(src);
% Get the coordinate IN THE AXES UNITS OF AXES1 (Here I chose to define them
% as pixels) of the point where the user clicked
cursorPoint = get(handles.axes1, 'CurrentPoint')
Step 2 : Check if the mouse click is inside the first image
Still in the getMousePositionOnImage function :
% Get the Position of the first image (We're only interested by the width
% and height of the axes1 object)
Img1Pos=get(handles.axes1,'Position')
% Check if inside
if(cursorPoint(1,1)<Img1Pos(3)&&cursorPoint(1,2)<Img1Pos(4)&&cursorPoint(1,1)>=0&&cursorPoint(1,2)>=0)
% Do stuff
end
Step 3 : If the click was inside the first image, update the position of the crossbar
% Check if inside
if(cursorPoint(1,1)<Img1Pos(3)&&cursorPoint(1,2)<Img1Pos(4)&&cursorPoint(1,1)>=0&&cursorPoint(1,2)>=0)
Lines=findobj('Type','line','Parent',handles.axes1);
if isempty(Lines)
% If Lines is empty, we need to create the line objects
line([0 Img1Pos(3)],[cursorPoint(1,2) cursorPoint(1,2)],'Color','g','Parent',handles.axes1);
line([cursorPoint(1,1) cursorPoint(1,1)],[0 Img1Pos(4)],'Color','g','Parent',handles.axes1);
else
% If not, we just update the fields XData and YData of both Lines
Lines(1).XData=[0 Img1Pos(3)]; % Unnecessary but I'll leave it there for clarity
Lines(1).YData=[cursorPoint(1,2) cursorPoint(1,2)];
Lines(2).XData=[cursorPoint(1,1) cursorPoint(1,1)];
Lines(2).YData=[0 Img1Pos(4)]; % Unnecessary but I'll leave it there for clarity
end
end
Result :
Now I'll let you do the last part, which involves linking the two crossbars. Instead of only checking if the click was on first image, you'll check both separately. Then, if it is in one of the image, you'll update both crossbar to the position of the click in the right axes object
Edit
The code below is inspired by the answer of BillBokeey, for which I'm very grateful.
First, do steps 0 and 1 of BillBokeey's solution. Then I put this function in my code:
function getMousePositionOnImage(src, event)
% Fetch the current handles structure
handles = guidata(src);
% Get the coordinate IN THE AXES UNITS OF AXES1 (Here I chose to define them
% as pixels) of the point where the user clicked
cursorPoint1 = get(handles.axes2, 'CurrentPoint');
cursorPoint2 = get(handles.axes3, 'CurrentPoint');
% Get the Position of the first image (We're only interested by the width
% and height of the axes1 object)
Img1Pos = getpixelposition(handles.axes2);
Img2Pos = getpixelposition(handles.axes3);
XLim1 = get(handles.axes2, 'XLim');
YLim1 = get(handles.axes2, 'YLim');
XLim2 = get(handles.axes3, 'XLim');
YLim2 = get(handles.axes3, 'YLim');
% Check if inside
if (cursorPoint1(1)<XLim1(2) && cursorPoint1(2)<YLim1(2) && cursorPoint1(1)>=XLim1(1) && cursorPoint1(1)>=YLim1(1))
Lines1=findobj('Type','line','Parent',handles.axes2);
Lines2=findobj('Type','line','Parent',handles.axes3);
if isempty(Lines1)
% If Lines is empty, we need to create the line objects
line(XLim1,[cursorPoint1(1,2) cursorPoint1(1,2)],'Color','g','Parent',handles.axes2);
line([cursorPoint1(1,1) cursorPoint1(1,1)],YLim1,'Color','g','Parent',handles.axes2);
line(XLim2,[cursorPoint1(1,2) cursorPoint1(1,2)],'Color','g','Parent',handles.axes3);
line([cursorPoint1(1,1) cursorPoint1(1,1)],YLim2,'Color','g','Parent',handles.axes3);
else
% If not, we just update the fields XData and YData of both Lines
Lines1(1).XData=XLim1; % Unnecessary but I'll leave it there for clarity
Lines1(1).YData=[cursorPoint1(1,2) cursorPoint1(1,2)];
Lines1(2).XData=[cursorPoint1(1,1) cursorPoint1(1,1)];
Lines1(2).YData=YLim1; % Unnecessary but I'll leave it there for clarity
Lines2(1).XData=XLim2; % Unnecessary but I'll leave it there for clarity
Lines2(1).YData=[cursorPoint1(1,2) cursorPoint1(1,2)];
Lines2(2).XData=[cursorPoint1(1,1) cursorPoint1(1,1)];
Lines2(2).YData=YLim2; % Unnecessary but I'll leave it there for clarity
end
elseif (cursorPoint2(1)<XLim2(2) && cursorPoint2(2)<YLim2(2) && cursorPoint2(1)>=XLim2(1) && cursorPoint2(1)>=YLim2(1))
Lines1=findobj('Type','line','Parent',handles.axes2);
Lines2=findobj('Type','line','Parent',handles.axes3);
if isempty(Lines2)
% If Lines is empty, we need to create the line objects
line(XLim1,[cursorPoint2(1,2) cursorPoint2(1,2)],'Color','g','Parent',handles.axes2);
line([cursorPoint2(1,1) cursorPoint2(1,1)],YLim1,'Color','g','Parent',handles.axes2);
line(XLim2,[cursorPoint2(1,2) cursorPoint2(1,2)],'Color','g','Parent',handles.axes3);
line([cursorPoint2(1,1) cursorPoint2(1,1)],YLim2,'Color','g','Parent',handles.axes3);
else
% If not, we just update the fields XData and YData of both Lines
Lines1(1).XData=XLim1; % Unnecessary but I'll leave it there for clarity
Lines1(1).YData=[cursorPoint2(1,2) cursorPoint2(1,2)];
Lines1(2).XData=[cursorPoint2(1,1) cursorPoint2(1,1)];
Lines1(2).YData=YLim1; % Unnecessary but I'll leave it there for clarity
Lines2(1).XData=XLim2; % Unnecessary but I'll leave it there for clarity
Lines2(1).YData=[cursorPoint2(1,2) cursorPoint2(1,2)];
Lines2(2).XData=[cursorPoint2(1,1) cursorPoint2(1,1)];
Lines2(2).YData=YLim2; % Unnecessary but I'll leave it there for clarity
end
end

How to check if a uicontextmenu is visible or active

What I have:
In a Matlab-GUI I have a uicontextmenu connected to a plot (=axes). If I "activate" this via a mouse-click (right-button), I can use the usual "Callback" to do something, like highlighting the plot. If the user then selects one of the uimenu-elements of the menu, I can use the Callback of this uimenu-element and reset the highlighting.
But there is a problem, if the user does not select an element. The context-menu disappears and I cannot find a way to find out, if this happens. In my example, the highlighted plot stays highlighted.
What I tried so far:
Besides reading the docs, I appended listeners to the properties to some of the uimenu-elements, e.g.:
addlistener(mymenu_element, 'Visible', 'PostSet', #mytest);
but this property, as well as others, seems not to be changed or touched at any time - what suprises me a bit :o
So the question is:
Is there a way to execute a function after a uicontextmenu is executed (or however you call it, when a context-menu "disappears")? In other words: if the user does not select an element of a context-menu, how can this be identified?
Since you cant listen to these items (I've run a few tests and come to the same conclusion) you can work around this by creating and managing your uicontextmenu in a different way:
function yourFunction
% create a figure
hFig = figure;
% add a listener to the mouse being pressed
addlistener ( hFig, 'WindowMousePress', #(a,b)mouseDown(hFig) );
end
function mouseDown(hFig)
% react depening on the mouse selection type:
switch hFig.SelectionType
case 'alt' % right click
% create a uicontext menu and store in figure data
hFig.UserData.uic = uicontextmenu ( 'parent', hFig );
% create the menu items for the uicontextmenu
uimenu ( 'parent', hFig.UserData.uic, 'Label', 'do this', 'Callback', #(a,b)DoThis(hFig) )
% assign to the figure
hFig.UIContextMenu = hFig.UserData.uic;
% turn visible on and set position
hFig.UserData.uic.Visible = 'on';
hFig.UserData.uic.Position = hFig.CurrentPoint;
% uicontext menu will appear as desired
% the next mouse action is then to either select an item or
% we will capture it below
otherwise
% if the uic is stored in the userdata we need to run the clean up
% code since the user has not clicked on one of the items
if isfield ( hFig.UserData, 'uic' )
DoThis(hFig);
end
end
end
function DoThis(hFig)
% Your code
disp ( 'your code' );
% Clean up
CleanUp(hFig);
end
function CleanUp(hFig)
% delete the uicontextmenu and remove the reference to it
delete(hFig.UserData.uic)
hFig.UserData = rmfield ( hFig.UserData, 'uic' );
end

how to prevent uimenu (MATLAB) from disappearing when checked

I have added uicontextmenu to the line object. uicontextmenu includes 3 check boxes. whenever I check any of them uicontextmenu disappears. I want uicontextmenu visible for sometime so that i can check multiple boxes and see the change (same as a button group but in uicontextmenu). Is there any solution to this or some other approach?
cmenu=uicontextmenu;
set(he,'uicontextmenu',cmenu);
item1=uimenu(cmenu,'label','Data A','checked','off','callback',#func_a);
item2=uimenu(cmenu,'label','Data B','checked','off','callback',#func_b);
item3=uimenu(cmenu,'label','Data C','checked','off','callback',#func_c);
basically, he is the line object created by plot(x,y) and func_a, func_b, func_c are function to convert property 'checked' to on|off.
This example is greatly inspired by Benoit_11 solution, but a bit refined. I was also under the impression that the 3 different functions in your callback were doing different things so I made the 3 different menus change different properties of the line (instead of changing the same property with different values).
I made the uimenu callback in one single nested function. It decides what to do based on the parameter what2do supplied at the uimenu definition (but feel free to keep 3 separate functions). However, note that the function that toggle the check mark is the same for all uimenu (you don't need a separate function for each of them).
function hf = TestUiContext2
%// Extension of Benoit_11 solution
clear ; clc ; close all
hf = figure ; %// return the handle of the figure
hax = axes; %// Create axes and save handle
plot(rand(20,3)); %// Plot three lines
hcmenu = uicontextmenu; %// Define a context menu; it is not attached to anything
%// Define the context menu items and install their callbacks
item1 = uimenu(hcmenu, 'Label','Bold line' , 'Callback' , {#uiCallback,'bold'} );
item2 = uimenu(hcmenu, 'Label','Dotted line' , 'Callback' , {#uiCallback,'dots'} );
item3 = uimenu(hcmenu, 'Label','Markers on' , 'Callback' , {#uiCallback,'mark'} );
hlines = findall(hax,'Type','line'); %// Locate line objects
for line = 1:length(hlines) %// Attach the context menu to each line
set(hlines(line),'uicontextmenu',hcmenu)
end
function uiCallback(obj,~,what2do)
hline = gco ;
switch what2do
case 'bold'
toggle_bold_line(hline)
case 'dots'
toggle_dotted_line(hline)
case 'mark'
toggle_markers(hline)
end
%// reposition the context menu and make it visible
set(hcmenu,'Position',get(gcf,'CurrentPoint'),'Visible','on')
toggle_checkmark(obj) %// toggle the checkmark
end
function toggle_checkmark(obj)
if strcmp(get(obj,'Checked'),'on')
set(obj,'Checked','off')
else
set(obj,'Checked','on')
end
end
function toggle_bold_line(hline)
if get(hline,'LineWidth')==0.5
set(hline,'LineWidth',2)
else
set(hline,'LineWidth',0.5)
end
end
function toggle_dotted_line(hline)
if strcmpi(get(hline,'LineStyle'),':')
set(hline,'LineStyle','-')
else
set(hline,'LineStyle',':')
end
end
function toggle_markers(hline)
if strcmpi(get(hline,'Marker'),'none')
set(hline,'Marker','o')
else
set(hline,'Marker','none')
end
end
end
Now you can enjoy ticking all your menu in one go ;)
Here is a workaround which might do the trick for you. That's not too elegant but it seems to work.
The trick is to set the menu 'Visible' property to 'on' in every callback you have (i.e. #func_a, #funct_b and #funct_c). When I run the following example (based on the demo on the Mathworks website) the menu does not disappear when the selection is changed. Notice that I created separate functions for each callback.
Here is the code:
function TestUiContext( ~)
%// Based on example from The Mathworks
%// http://www.mathworks.com/help/matlab/ref/uicontextmenu.html
clear
clc
close all
%// Create axes and save handle
hax = axes;
%// Plot three lines
plot(rand(20,3));
%// Define a context menu.
hcmenu = uicontextmenu;
%// Define the context menu items and install their callbacks
item1 = uimenu(hcmenu,'Label','dashed','Callback',#(s,e) hcb1);
item2 = uimenu(hcmenu,'Label','dotted','Callback',#(s,e) hcb2);
item3 = uimenu(hcmenu,'Label','solid','Callback',#(s,e) hcb3);
%// Locate line objects
hlines = findall(hax,'Type','line');
%// Attach the context menu to each line
for line = 1:length(hlines)
set(hlines(line),'uicontextmenu',hcmenu)
end
%// In the callback of every item/option, set the menu property 'Visible' to 'on'.
function hcb1
set(gco,'LineStyle','--');
set(hcmenu,'Visible','on')
end
function hcb2
set(gco,'LineStyle',':');
set(hcmenu,'Visible','on')
end
function hcb3
set(gco,'LineStyle','-');
set(hcmenu,'Visible','on')
end
end
And 2 screenshots to show what it looks like:
And moving the cursor down:
So as I said, not perfect but hopefully it will do the job for you!

Within Matlab GUI, I want to transfer the selected data in listbox1 to listbox2

This case is, I know how to transfer all the data from one listbox to another one, and then clear listbox1. I did in this way:
function pushbutton1_Callback(hObject, eventdata, handles)
StringInLB1 = get(handles.listbox1,'string');
set(handles.listbox2,'string',StringInLB1);
set(handles.listbox1,'string','');
Now my question is: How can I transfer some selected data to listbox2 ? I "ctrl+ single click" multiple data in listbox1, but how can I use thoes data?
Thanks a lot.
It looks like you are looking to assign some function to the listbox callback. i.e. each time the selection is changed by the user, you want to do something with the data. Anyhow that's what I understood from your comment to #James answer.
If it's the case, here is a sample code generating a simple GUI in which the color of a plot is changed by the user directly by clicking in the listbox:
function DummyListBox
global hFig hListBox hPlot
ScreenSize = get(0,'ScreenSize');
hFig = figure('Visible','off','Position',[ScreenSize(3)/2,ScreenSize(4)/2,450,285]);
ColorString = {'Red';'Green';'Blue'}; % Define string populating the listbox
hListBox = uicontrol('Style','Listbox','String',ColorString,'Position',[315,150,70,50],'max',3,...
'min',1,'Callback',#ListBox_Callback); %%// added 'min' and 'max' properties to select multiple items
hText = uicontrol('Style','Text','Position',[315,220,70,35],'String','Empty now'); %%// Add text box
hAxes = axes('Units','Pixels','Position',[50,60,200,185]);
set(hFig,'Visible','on')
x = 1:5*pi;
hPlot = plot(x,sin(x),'-r','Parent',hAxes); %display some data
% Listbox callback: each time the selection changes, the color of the
% plot changes accordingly.
function ListBox_Callback(~,~)
SelectedValues = get(hListBox,'Value'); % Get the values selected
set(hText,'String',SelectedValues); % Uptdate the string in the textbox
NewColor = ColorString{get(hListBox,'Value')};
set(hPlot,'Color',NewColor)
end
end
The output now looks like this:
As you see the function in which the change in color takes place is the listbox's callback. Hope this is what you want. If not please be more specific as to what you would like. Thanks!
EDIT: As you can see, I added a text box to show you what it looks like when you select multilpe items in the listbox. In order to do so, you must add a 'min' and a 'max' property when you create the listbox. I set them to 1 and 3, respectively. The 'Value' property of the listbox corresponds to the actual number of the item selected from the list. So if you have 3 items as is my exmample,the Value can be 1,2,3 or any combination. They are displayed in the text box. You can get the corresponding data from those values, stored in vectors.
So to answer your question, you could write:
Data1 = ValuesVector(1)
Data2 = VectorValues(2)
... and so on
If you use a
tmp = get(handles.listbox1,'string');
You'll see what the variable is like. Then you can use something like
set(handles.listbox1,'string',tmp{2});