Modifying uitable column header alignment? - matlab

I'm updating my generic data manipulation GUI and I thought I would make use of a uitable instead of a uicontrol listbox to display the path(s) and filename(s) of the loaded files. I noticed the column headers are centered by default, and I can't seem to figure out how to left align them if the filename is longer than the width of the table. I checked the properties returned by get and poked around with findjobj but didn't find anything obvious. My attempt to use an html string was also unsuccessful.
Am I missing something obvious?
Sample code:
function testcode
res = get(0,'ScreenSize');
figdim = [1280 720]; % Main figure size, pixels
handles.mainfig = figure( ...
'Units', 'Pixels', ...
'Position', [(res(3) - figdim(1))/2 (res(4) - figdim(2))/2 figdim(1) figdim(2)], ...
'Name', 'Meep', ...
'Resize', 'off', ...
'DockControls', 'off', ...
'NumberTitle', 'off', ...
'Toolbar', 'figure' ...
);
handles.filetable = uitable( ...
'Parent', handles.mainfig, ...
'Units', 'normalized', ...
'Position', [0.038 0.71 0.235 0.1], ...
'ColumnName', 'File Loaded', ...
'Data', {'None'} ...
);
handles.loadfilebtn = uicontrol( ...
'Parent', handles.mainfig, ...
'Style', 'pushbutton', ...
'Units', 'normalized', ...
'Position', [0.075 0.85 0.16 0.07], ...
'String', 'This is a Button, Click', ...
'Callback', {#abutton} ...
);
guidata(handles.mainfig, handles);
end
function abutton(hObject,~)
handles = guidata(hObject);
filepath = 'C:\folder\folder\folder\folder\folder\folder\folder\folder\folder\superduperreallylongfilename.fileextension';
set(handles.filetable,'Data',{filepath});
set(handles.filetable,'ColumnWidth',{length(filepath)*5});
test = '<html><left />File Loaded</html>';
set(handles.filetable,'ColumnName',test); % Attempt #1, doesn't work
end

The easiest workaround I could think of, is to split table and table-header in two separate uitables. It's a little fiddly to set the position vectors properly, but generally it works like a charm. Important: place the "header-uitable" below (in code first) the "data-table". You save some trouble this way.
function testcode
close all
res = get(0,'ScreenSize');
figdim = [1280 720]; % Main figure size, pixels
handles.mainfig = figure( ...
'Units', 'Pixels', ...
'Position', [(res(3) - figdim(1))/2 (res(4) - figdim(2))/2 figdim(1) figdim(2)], ...
'Name', 'Meep', ...
'Resize', 'off', ...
'DockControls', 'off', ...
'NumberTitle', 'off', ...
'Toolbar', 'figure' ...
);
handles.tableheader = uitable( ...
'Parent', handles.mainfig, ...
'Units', 'normalized', ...
'Position', [0.038 0.71 0.235 0.1], ...
'ColumnName', 'File Loaded', ...
'ColumnWidth', {271} ...
);
handles.filetable = uitable( ...
'Parent', handles.mainfig, ...
'Units', 'normalized', ...
'Position', [0.038 0.682 0.235 0.1], ...
'ColumnName', {}, ...
'Data', {'None'} ...
);
handles.loadfilebtn = uicontrol( ...
'Parent', handles.mainfig, ...
'Style', 'pushbutton', ...
'Units', 'normalized', ...
'Position', [0.075 0.85 0.16 0.07], ...
'String', 'This is a Button, Click', ...
'Callback', {#abutton} ...
);
guidata(handles.mainfig, handles);
end
function abutton(hObject,~)
handles = guidata(hObject);
filepath = 'C:\folder\folder\folder\folder\folder\folder\folder\folder\folder\superduperreallylongfilename.fileextension';
set(handles.filetable,'Data',{filepath});
set(handles.filetable,'ColumnWidth',{length(filepath)*5});
end

You've mentioned findjobj, which allows you to get the JTable object behind your table. What you're actually trying to do is change some properties of the header, or JTableHeader object. Once you know what you're looking for the solution is easy to find (e.g. this previous thread).
In summary, put this code at the end of the button callback:
function abutton(hObject,~)
%// ....
hTable = handles.filetable;
jScrollpane = findjobj(hTable);
jTable = jScrollpane.getComponent(0).getComponent(0);
%// You can see the list of components using findjobj(hTable).list
%// Part1:
dtcr = javaObject('javax.swing.table.DefaultTableCellRenderer');
dtcr.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
jTable.getColumn(jTable.getColumnName(0)).setHeaderRenderer(dtcr);
%// Part2:
jRenderer = jTable.getTableHeader().getDefaultRenderer();
jRenderer.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
%// Finalization:
jTable.invalidate();
end
Clicking the button then results in:
Several notes:
Here's the documentation of SwingConstants.
The two Parts are supposed to be two different solutions, but I noticed that it only works if both are present (I have no explanation for this).
I tried setting this in the figure creation function, but that didn't work (I have no explanation for this either).
Tested on MATLAB 2014a.

Related

Share information between two (app disigner) apps

Can anyone please tell me how can i make my main app open a secondary app which will capture some values and then send them back to my main app?
I'm aware that this issue is tackled in app designer documentation, but I have been unable to implement those steps successfully. Also, I tried to run the example but Matlab says the file doesn't exists. If anyone could please share that example it would be also very helpful.
I have never tried to implement this on my own, but I often wandered myself how I could accomplish this if facing a complex apps architecture.
Actually, if you instantiate two GUIs in the same script/function, or if you have one GUI creating another GUI inside one of its functions, the simplest way would be to play with function handles. For example, the first GUI can pass a function handle defined among its functions to the target GUI's constructor and, this way, the target GUI can invoke it in order to modify the first GUI's data and/or properties when necessary.
The standard approach, anyway, which is considered as a best practice, works as follows. Let's assume that you have two GUIs named G1 and G2 and that they are distinct (you are not running two instances of the same GUI). If they are both visible (HandleVisibility set to on) and they both have a Tag identifier defined (G1 and G2 in our example), you can search for them within the Matlab "workspace". Hence:
% This is a G2 event handler
function pushbutton1_Callback(hObject, eventdata, handles)
g1_h = findobj('Tag','G1');
if (~isempty(g1_h))
% get all data associated to G1
g1_data = guidata(g1_h);
% modify a G2 object based on a G1 object
set(handles.MyTextBox,'String',get(g1_data.MyEditBox,'String'));
end
end
MATLAB's App Designer generates class-based GUI's rather than GUIDE's function-based GUIs. The advantage of this approach is that we can pass the GUIs around as objects rather than having to get creative with things like function returns or searching for objects by tag.
Here's a simple programmatic example that illustrates one approach to this concept. The main figure window opens a secondary prompt window, which provides two inputs. When the prompt window is closed, the primary GUI prints the input values to the command window and exits.
The main window:
classdef mainwindow < handle
properties
mainfig
butt
end
methods
function [self] = mainwindow()
% Build a GUI
self.mainfig = figure('Name', 'MainWindow', 'Numbertitle', 'off', ...
'MenuBar', 'none', 'ToolBar', 'none');
self.butt = uicontrol('Parent', self.mainfig, 'Style', 'Pushbutton', ...
'Units', 'Normalized', 'Position', [0.1 0.1 0.8 0.8], ...
'String', 'Push Me', 'Callback', #(h,e) self.buttoncallback);
end
function buttoncallback(self)
tmpwindow = subwindow(); % Open popupwindow
uiwait(tmpwindow.mainfig); % Wait for popup window to be closed
fprintf('Parameter 1: %u\nParameter 2: %u\n', tmpwindow.parameter1, tmpwindow.parameter2);
close(self.mainfig);
end
end
end
The sub window:
classdef subwindow < handle
properties
mainfig
label1
box1
label2
box2
closebutton
parameter1
parameter2
end
methods
function [self] = subwindow()
% Build a GUI
self.mainfig = figure('Name', 'SubWindow', 'Numbertitle', 'off', ...
'MenuBar', 'none', 'ToolBar', 'none');
self.label1 = uicontrol('Parent', self.mainfig, 'Style', 'text', ...
'Units', 'Normalized', 'Position', [0.4 0.7 0.2 0.05], ...
'String', 'Parameter 1');
self.box1 = uicontrol('Parent', self.mainfig, 'Style', 'edit', ...
'Units', 'Normalized', 'Position', [0.4 0.6 0.2 0.1], ...
'String', '10');
self.label2 = uicontrol('Parent', self.mainfig, 'Style', 'text', ...
'Units', 'Normalized', 'Position', [0.4 0.4 0.2 0.05], ...
'String', 'Parameter 2');
self.box2 = uicontrol('Parent', self.mainfig, 'Style', 'edit', ...
'Units', 'Normalized', 'Position', [0.4 0.3 0.2 0.1], ...
'String', '10');
self.closebutton = uicontrol('Parent', self.mainfig, 'Style', 'Pushbutton', ...
'Units', 'Normalized', 'Position', [0.4 0.1 0.2 0.1], ...
'String', 'Close Window', 'Callback', #(h,e) self.closewindow);
end
function closewindow(self)
% Drop our input parameters into this window's properties
self.parameter1 = str2double(self.box1.String);
self.parameter2 = str2double(self.box2.String);
% Close the window
close(self.mainfig);
end
end
end

uicontrol callback for more than one function

I want to write a GUI program in Matlab and make 3 sliders on it with uicontrol and then write callbacks to use their 3 values in one command. I found a way to write a function for one slider as you can see in my program. Can you help me how use these 3 callbacks? (I use R2014a)
sld = uicontrol('Style', 'slider',...
'Min',0,'Max',255,'Value',0,...
'Position', [400 20 120 20],...
'Callback', #Blue);
sld = uicontrol('Style', 'slider',...
'Min',0,'Max',255,'Value',0,...
'Position', [400 60 120 20],...
'Callback', #Green);
sld = uicontrol('Style', 'slider',...
'Min',0,'Max',255,'Value',255,...
'Position', [400 100 120 20],...
'Callback', #Red);
function Red(source,~)
R = get(source,'Value');
end
function Green(source,~)
G = get(source,'Value');
end
function Blue(source,~)
B = get(source,'Value');
end
RGB = cat(3,R,G,B); %??????
Error: Undefined function or variable "R".
Persistent data must be stored somewhere that's accessible to the callback function. One common technique is to use the parent figure's UserData field. In the example above, once a slider has been moved, the current colour can be found in the RGB field of figure fh's UserData. Also, here only one callback is used, and the UI item is identified via its Tag.
fh = figure(1);
clf
%// Initialize figure's UserData
set(fh, 'UserData', struct('RGB', [0 0 0]));
sld_b = uicontrol('Style', 'slider',...
'Min',0,'Max',255,'Value',0,...
'Position', [400 20 120 20],...
'Callback', #colourHandler, 'Tag', 'blue');
sld_g = uicontrol('Style', 'slider',...
'Min',0,'Max',255,'Value',0,...
'Position', [400 60 120 20],...
'Callback', #colourHandler, 'Tag', 'green');
sld_r = uicontrol('Style', 'slider',...
'Min',0,'Max',255,'Value',255,...
'Position', [400 100 120 20],...
'Callback', #colourHandler, 'Tag', 'red');
%// This is in a separate file, colourHandler.m
function colourHandler(source, ~)
%// Find which slider triggered us
if strcmpi(get(source, 'Tag'), 'red')
ind = 1;
elseif strcmpi(get(source, 'Tag'), 'green')
ind = 2;
else
ind = 3;
end
%// update UserData
ud = get(get(source, 'Parent'), 'UserData');
ud.RGB(ind) = get(source, 'Value');
set(get(source, 'Parent'), 'UserData', ud);
end
Alternatively you can just store the handles of your graphics objects and use those to obtain the values in other functions.
For example:
function testcode
% Initialize sample GUI
h.fig = figure( 'MenuBar', 'none', 'ToolBar', 'none');
h.sld(1) = uicontrol( ...
'Parent', h.fig, ...
'Style', 'slider',...
'Min', 0, 'Max', 255, 'Value', 0, ...
'Units', 'Normalized', 'Position', [0.1 0.65 0.4 0.1], ...
'Tag', 'Red' ...
);
h.sld(2) = uicontrol( ...
'Parent', h.fig, ...
'Style', 'slider', ...
'Min', 0, 'Max', 255, 'Value', 0, ...
'Units', 'Normalized', 'Position', [0.1 0.45 0.4 0.1], ...
'Tag', 'Green' ...
);
h.sld(3) = uicontrol( ...
'Parent', h.fig, ...
'Style', 'slider', ...
'Min', 0, 'Max', 255, 'Value', 255, ...
'Units', 'Normalized', 'Position', [0.1 0.25 0.4 0.1], ...
'Tag', 'Blue' ...
);
% Use an axes object as a color display box
% Get starting RGB values for the color display, normalized so 0 <= x <= 1
startRGB = [get(h.sld(1), 'Value'), get(h.sld(2), 'Value'), get(h.sld(3), 'Value')]/255;
h.ax = axes( ...
'Parent', h.fig, ...
'Units', 'Normalized', 'Position', [0.6 0.36 0.3 0.3], ...
'XTickLabels', '', 'YTickLabels', '', ...
'Color', startRGB ...
);
% Need to set callback after all our elements are initialized
nsliders = length(h.sld);
set(h.sld, {'Callback'}, repmat({{#slidercallback, h}}, nsliders, 1));
end
function slidercallback(~, ~, h)
% Update background color of our axes object every time the slider is updated
RGB = [get(h.sld(1), 'Value'), get(h.sld(2), 'Value'), get(h.sld(3), 'Value')]/255;
set(h.ax, 'Color', RGB');
end
When callbacks execute they are passed 2 inputs by default, the invoking object and a structure of event data. As explained in the callback documentation, you can pass additional inputs to your callback by wrapping everything into a cell array. One thing to note is that the value of your variable being passed to the callback is its value as it exists when you define the callback. In other words, if we set the callback for our sliders at the same time we create them, when the callback for Red is fired h will only contain a handle to our figure, when the callback for Green is fired h will contain a handle to our figure and to the Red slider, and so on.
Because of this, you will see I have defined the callbacks once we have initialized all of our graphics objects. Using the curly brackets to set properties of multiple objects is explained in MATLAB's documentation for set. I use repmat so the size of the cell array is the same size as our array of slider objects.

MATLAB: How to pass data between 2 GUIs using the findobj-Function?

i am new to creating GUIs with Matlab. I have one MainGui from which i open a Subgui so that the user can click checkboxes. After clicking the okay-Button my Subgui closes and one sees the MainGui-surface again.
How can i access the clickbutton value without using getappdata and setappdata and instead doing it with findobj-function which when it works is far easier for me.
So i am in the MainGui code and i look for the Subgui with
hGui = findobj('Tag','Subgui');
where 'Subgui' is the value of the Tag property of the SubGUI. Handles visibility is on for both!!
% get control handles for this GUI
handlesSubgui = guidata(hGui);
% now read the data from the checkbox
checkValue = get(handlesSubgui.checkbox1,'Value');
Why doesnt it work? i set the correct Tags and handle visilility is on but i get
hGui =
Empty matrix: 0-by-1
!?
Has anyone an idea? i would be glad to get help!
Best regards, John
One option to consider for a case like this is to initialize a small GUI inside your button callback. To illustrate, I'll set up a little programmatic GUI:
function testcode
res = get(0,'ScreenSize');
figdim = [300 300]; % Figure size, pixels
h.mainfig = figure( ...
'Units', 'Pixels', ...
'Position', [(res(3) - figdim(1))/2 (res(4) - figdim(2))/2 figdim(1) figdim(2)], ...
'Name', 'This is the Main GUI', ...
'Resize', 'off', ...
'DockControls', 'off', ...
'NumberTitle', 'off', ...
'MenuBar', 'none', ...
'Toolbar', 'none' ...
);
h.subGUIbutton = uicontrol( ...
'Parent', h.mainfig, ...
'Units', 'Normalized', ...
'Position', [0.25 0.6 0.5 0.3], ...
'String', 'Open Checkbox GUI' ...
);
h.displaydatabutton = uicontrol( ...
'Parent', h.mainfig, ...
'Units', 'Normalized', ...
'Position', [0.25 0.1 0.5 0.3], ...
'String', 'Display Checkbox Selections' ...
);
% Requires R2014b or newer, otherwise we'll have to use set
try
h.subGUIbutton.Callback = {#checkboxGUI, h};
h.displaydatabutton.Callback = {#displaydata, h};
catch
set(h.subGUIbutton, 'Callback', {#checkboxGUI, h});
set(h.displaydatabutton, 'Callback', {#displaydata, h});
end
And our callbacks will be structured like this:
function checkboxGUI(~, ~, handles)
res = get(0,'ScreenSize');
figdim = [200 200]; % Figure size, pixels
h2.mainfig = figure( ...
'Units', 'Pixels', ...
'Position', [(res(3) - figdim(1))/2 (res(4) - figdim(2))/2 figdim(1) figdim(2)], ...
'Name', 'This is the Sub GUI', ...
'Resize', 'off', ...
'DockControls', 'off', ...
'NumberTitle', 'off', ...
'MenuBar', 'none', ...
'Toolbar', 'none' ...
);
% Build some checkboxes
for ii = 1:4
h2.checkbox(ii) = uicontrol( ...
'Parent', h2.mainfig, ...
'Style', 'checkbox', ...
'Units', 'Normalized', ...
'Position', [0.25 (1 - ii*0.15) 0.5 0.1], ...
'String', sprintf('Checkbox #%u', ii) ...
);
end
h2.closebutton = uicontrol( ...
'Parent', h2.mainfig, ...
'Style', 'pushbutton', ...
'Units', 'Normalized', ...
'Position', [0.25 0.15 0.5 0.1], ...
'String', 'Accept Changes', ...
'Callback', {#closecheckbox} ...
);
function closecheckbox(~, ~)
% requires R2014b or newer for dot notation
try
test = find([h2.checkbox(:).Value]); % Returns ID of checked boxes
catch
test = find(cell2mat(get(h2.checkbox(:), 'Value'))'); % Returns ID of checked boxes
setappdata(handles.mainfig, 'BoxesChecked', test);
close(h2.mainfig);
end
waitfor(h2.mainfig); % Wait for user to close the checkbox GUI
end
function displaydata(~, ~, handles)
BoxesChecked = getappdata(handles.mainfig, 'BoxesChecked');
if isempty(BoxesChecked)
fprintf('No boxes celected\n');
else
fprintf('User selected box: %d\n', BoxesChecked);
end
end
Note that I've used a nested function for readability. In this simple example we have two buttons in our main GUI, a button to open the user prompt and then a display button. When the user opens the checkbox prompt, execution of all GUI commands pauses until the prompt is closed. When the display button is clicked, we get the checked values from the app data and print them to the command window.

Different settings using GUI pop-up button (matlab)

So I've been working on this GUI for a while, and recently finished a prototype where you could input resistance, inductance, and capacitance to get the quality factor, and the resonance frequency using equations. What I want to do now is to make a GUI that accepts different sets to find the missing values. ex: given q-factor, resistance, and inductance. I've made a pop-up button that I want to switch depending on what the user has to input. I'm new to GUI programming, so I was wondering if making a giant If statement following the pop-up button is the simplest way. ex: if (RCL)... if(QLR)... of course this would make me type out tons of code for each setting, so I was wondering if there was an easier way. Thanks in advance.
In my gui I have 5 edit text boxes, a popup button, and a graph. what I want to happen is for the user to choose which setting they have depending on the inputs that they have, and for the remaining edit boxes to display the other values. so if they select the RCL setting, I want the program to take the RCL inputs, and calculate W and Q, then show them in the proper edit text boxes. I want this to be malleable though, so I have made several other settings such as CLW or QRL that the user can choose from, so they can get the missing variables. the graph will be a bode plot of a transfer function using the RCL values. the main problem is that for each setting, different forms of the same equation are used to calculate the missing values. Is there a way to call the function I need for a specific case and get the values everywhere I need them?
I'm still not 100% following what you're looking for, but hopefully this gets you going in the right direction:
function testGUI
% Initialize dummy GUI
h.mainwindow = figure( ... % Main figure window
'Units','pixels', ...
'Position',[100 100 400 400], ...
'MenuBar','none', ...
'ToolBar','none', ...
'Resize', 'off' ...
);
h.var1box = uicontrol( ...
'Parent', h.mainwindow, ...
'Style', 'edit', ...
'Units', 'Normalized', ...
'Position', [0.4 0.5 0.2 0.1], ...
'String', '0' ...
);
h.var2box = uicontrol( ...
'Parent', h.mainwindow, ...
'Style', 'edit', ...
'Units', 'Normalized', ...
'Position', [0.4 0.3 0.2 0.1], ...
'String', '0' ...
);
h.var3box = uicontrol( ...
'Parent', h.mainwindow, ...
'Style', 'edit', ...
'Units', 'Normalized', ...
'Position', [0.4 0.1 0.2 0.1], ...
'String', '', ...
'Enable', 'off' ...
);
h.var1label = uicontrol( ...
'Parent', h.mainwindow, ...
'Style', 'text', ...
'Units', 'Normalized', ...
'Position', [0.4 0.6 0.2 0.05], ...
'String', 'Var1' ...
);
h.var2label = uicontrol( ...
'Parent', h.mainwindow, ...
'Style', 'text', ...
'Units', 'Normalized', ...
'Position', [0.4 0.4 0.2 0.05], ...
'String', 'Var2' ...
);
h.var3label = uicontrol( ...
'Parent', h.mainwindow, ...
'Style', 'text', ...
'Units', 'Normalized', ...
'Position', [0.4 0.2 0.2 0.05], ...
'String', 'Var3' ...
);
h.dropdown = uicontrol( ...
'Parent', h.mainwindow, ...
'Style', 'popupmenu', ...
'Units', 'Normalized', ...
'Position', [0.3 0.7 0.4 0.05], ...
'String', {'Var1'; 'Var2'; 'Var3'}, ...
'Value', 3, ...
'Callback', {#vartoggle, h} ...
);
h.dropdownlabel = uicontrol( ...
'Parent', h.mainwindow, ...
'Style', 'text', ...
'Units', 'Normalized', ...
'Position', [0.3 0.75 0.4 0.05], ...
'String', 'Variable to solve for?' ...
);
h.calculatebutton = uicontrol( ...
'Parent', h.mainwindow, ...
'Style', 'pushbutton', ...
'Units', 'Normalized', ...
'Position', [0.72 0.7 0.2 0.05], ...
'String', 'Calculate', ...
'Callback', {#calcdata, h} ...
);
end
function vartoggle(eventdata, ~, h)
% Execute on change in dropdown menu, enable/disable edit boxes
% appropriately
switch eventdata.Value
case 1
% Solve for variable 1
h.var1box.Enable = 'off';
h.var2box.Enable = 'on';
h.var3box.Enable = 'on';
h.var1box.String = '';
h.var2box.String = '0';
h.var3box.String = '0';
case 2
% Solve for variable 2
h.var1box.Enable = 'on';
h.var2box.Enable = 'off';
h.var3box.Enable = 'on';
h.var1box.String = '0';
h.var2box.String = '';
h.var3box.String = '0';
case 3
% Solve for variable 3
h.var1box.Enable = 'on';
h.var2box.Enable = 'on';
h.var3box.Enable = 'off';
h.var1box.String = '0';
h.var2box.String = '0';
h.var3box.String = '';
end
end
function calcdata(~, ~, h)
% Execute when calculate button is pressed, calculate missing value
% Asssumes Var1 + Var2 = Var 3 for this example
switch h.dropdown.Value
case 1
% Solve for variable 1
B = str2double(h.var2box.String);
C = str2double(h.var3box.String);
A = C - B;
h.var1box.String = A;
case 2
% Solve for variable 2
A = str2double(h.var1box.String);
C = str2double(h.var3box.String);
B = C - A;
h.var2box.String = B;
case 3
% Solve for variable 3
A = str2double(h.var1box.String);
B = str2double(h.var2box.String);
C = A + B;
h.var3box.String = C;
end
end
What this does is allow the user to specify the quantity to solve for and updates your ability to modify the edit boxes accordingly. Once you click the calculate button the missing value is calculated.

One function for multiple pushbuttons in matlab

I was thinking of setting one function for multiple pushbuttons, They all do the same thing, but it has a different defining value. This is so that when one pushbutton is activated it does not get mixed up with the other pushbutton of the same function
See the documentation for callbacks. Callbacks accept two input arguments by default: the handle of the object that invoked the function and a structure of event data from the object, which may or may not be empty. You can use the String or Tag properties of your pushbutton to control behavior of your GUI based on what button is pressed using a single callback function. Consider the following example:
function testGUI
handles.mainwindow = figure();
handles.mytextbox = uicontrol( ...
'Style', 'edit', ...
'Units', 'normalized', ...
'Position', [0.15 0.80 .70 .10], ...
'String', 'No Button Has Been Pressed' ...
);
handles.button(1) = uicontrol( ...
'Style', 'pushbutton', ...
'Units', 'normalized', ...
'Position', [0.05 0.05 .30 .70], ...
'String', 'Button1', ...
'Callback', {#mybuttonpress,handles} ...
);
handles.button(2) = uicontrol( ...
'Style', 'pushbutton', ...
'Units', 'normalized', ...
'Position', [0.35 0.05 .30 .70], ...
'String', 'Button2', ...
'Callback', {#mybuttonpress,handles} ...
);
handles.button(3) = uicontrol( ...
'Style', 'pushbutton', ...
'Units', 'normalized', ...
'Position', [0.65 0.05 .30 .70], ...
'String', 'Button3', ...
'Callback', {#mybuttonpress,handles} ...
);
end
function mybuttonpress(src, ~, handles)
switch src.String
case 'Button1'
handles.mytextbox.String = 'Button 1 Has Been Pressed';
case 'Button2'
handles.mytextbox.String = 'Button 2 Has Been Pressed';
case 'Button3'
handles.mytextbox.String = 'Button 3 Has Been Pressed';
otherwise
% Something strange happened
end
end
Note that this requires MATLAB R2014b or newer in order to use the dot notation for accessing object properties. See this blog post for more information.
You can just define a generic function and call it from all of your push button callbacks