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.
Related
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!
I'm new to matlab and I was wondering if anyone could shed some light.
I am using Matlab R2017B and App Designer.
My question:
I have a panel in a GUI that is default set to invisible.
I also have a dropdown menu with a few values stored:
"Please Select"
"Panel 1"
"panel 2"
"panel 3"
etc...
when i select panel 1 from the dropdown I want that specific panel to then become visible. ie the callback:
function DropDownValueChanged(app, event)
app.Panel1.Visible = 'on';
this is all fine and works but I cant seem to get the next part right.
When i go back to the dropdown and select "panel 2" What i would like is for the program to then close the current panel and open the panel named "panel 2" by selecting that value from the dropdown.
Am I wrong in using visibility to define this?
How can i connect the dropdown values to corresponding panels in a more intuitive way?
I've been messing around with all sorts of tutorials but still cant get it to work
Thanks in advance
You need to read the value of the dropdown and use that to evaluate which panel should be displayed. Initially hide all the panels and then just make the panel of interest visible. My DropdownValueChanged function looks like this.
% Value changed function: DropDown
function DropDownValueChanged(app, event)
value = app.DropDown.Value;
% Hide all the panels
app.Panel1.Visible = 'off';
app.Panel2.Visible = 'off';
app.Panel3.Visible = 'off';
%If Panel 1 is selected, show panel 1
if strcmp(value,'Panel 1')
app.Panel1.Visible = 'on';
elseif strcmp(value,'Panel 2')
app.Panel2.Visible = 'on';
elseif strcmp(value,'Panel 3')
app.Panel3.Visible = 'on';
end
end
I also have the following in the startupFcn to hide all but the first panel.
% Code that executes after component creation
function startupFcn(app)
app.Panel1.Visible = 'on';
app.Panel2.Visible = 'off';
app.Panel3.Visible = 'off';
end
I have a matlab gui made with guide that has a checkbox uicontroll. When that checkbox is focused, pressing spacebar (un)checks that checkbox.
I don't want this behaviour - how can I turn this off?
I want to turn it off because I have defined a keypressFcn for the spacebar and I want something else to happen when the user presses the spacebar. atm that 'something else' is working. If spacebar is hit, my keypressFcn runs and does what it should do and additionally the checkbox (un)checks. I only want it to execute my keypressFcn, though..
I don't really know where to start solving this problem.. Just some generall direction instructions would already be helpfull!
when I had a similar problem I hacked the KeyPressFcn to bypass the spacebar:
function test_KeyPressFcn
% Create a figure
figure();
% Add a check box with a KeyPressFcn callback, which will be called when the user preses a key
uicontrol('Style' , 'checkBox','KeyPressFcn' , #KeyPressed);
function KeyPressed(src , event)
if strcmpi(event.Key , 'space')
% Pressing the spacebar changed the value of the checkbox to
% new_value
new_value = get(src , 'Value');
% Let's revert it to its old value
set(src , 'Value' , ~new_value)
end
The space bar is still working but you set the checkbox back to its original value!
I had the similar issue. My solution was to set up a dummy uicontrol (like a text Style with empty String), and in any uicontrol CallBack, I always call uicontrol(dummy) to have the dummy uicontrol focused, so spacebar press will have no effect. It doesn't sound a good solution, but it works well for me.
dummy = uicontrol(gcf, 'Style', 'text'); % use this for focus
ckbox = uicontrol(gcf, 'Style', 'CheckBox', 'String', 'myCheckBox', ...
'Callback', #(h,e)uicontrol(dummy), 'Position', [100 200 100 32]);
If you now click the checkbox, it will change its value, and the callback will move focus to the dummy text, so spacebar won't change its value anymore.
If user may press TAB key, it will cycle eligible uicontrols, and if focus lies on the checkbox, spacebar will change its value again. My solution for this is to do uicontrol(dummy) in the KeypressFcn so dummy will be on focus after TAB press.
I have a strange problem in my Matlab GUI. The GUI contains uipanel and icontrol objects, some of which are buttons. Usually, the GUI is controlled with the directional arrow keys.
However, once I click one of my buttons, the keyboard events are not recorded any more. I've set breakpoints in the keypress callback to find out what's happening and it turns out the callback is never called. If I manage to click the GUI background, it works once again, which makes me think it's related to the active control. But how can I give control back to the main window? uicontrol(hFigure) doesn't work, neither does figure(hFigure).
The following code snippet reproduces the problem. Copy it into a new file (ideally called test.m, otherwise Code Analyzer will complain) and run it to open a GUI window that shows this behaviour. Once the button is clicked, the arrow keys aren't recorded any more unless the user clicks the area outside the text uicontrol.
function test
figure('KeyPressFcn',#key)
clf
p = uipanel('position',[0 0 1 1],'BackgroundColor',[.7 .7 .7]);
uicontrol('Style','push','String','Click me','Units','norm',...
'Position',[0.43 0.91 0.14 0.06],'Callback',#button);
t = uicontrol(p,'Style','text','String','Use arrow keys','Units','norm',...
'Position',[0.2 0.4 0.6 0.2],'FontSize',20);
function button(~,~)
set(t,'String','Button pressed.');
end
function key(~,e)
set(t,'String',['Key ' e.Key ' pressed.']);
end
end
You are right about why this doesn't work. When you click on the button, the figure is no longer the active control. The best way to fix this is to additionally set the KeyPressFcn property of the button to be the same as the KeyPressFcn of the figure.
function test
figure('KeyPressFcn',#key)
clf
p = uipanel('position',[0 0 1 1],'BackgroundColor',[.7 .7 .7]);
uicontrol('Style','push','String','Click me','Units','norm',...
'Position',[0.43 0.91 0.14 0.06],'Callback',#button, ...
'KeyPressFcn', #key);
t = uicontrol(p,'Style','text','String','Use arrow keys','Units','norm',...
'Position',[0.2 0.4 0.6 0.2],'FontSize',20);
function button(~,~)
set(t,'String','Button pressed.');
end
function key(~,e)
set(t,'String',['Key ' e.Key ' pressed.']);
end
end
You could also set the WindowKeyPressFcn instead of KeyPressFcn.
For more information see my answer here:
matlab: difference between KeyPressFcn and WindowKeyPressFcn
I have a checkbox on GUI that draws a rectangle on a live video feed, however, I need the rectangle to dissapear or be deleted when I uncheck it.
does anyone have any idea how to do this?
This is my code, I have tried putting things in else, but nothing works.
function Box(hObject,eventdata)
if (((get(hObject,'Value') == get(hObject,'Max'))))
% Checkbox is checked-take appropriate action
hold on;
rectangle('Position',[50,50,100,100],'EdgeColor','r')
else
end
You need to save the handle created by the function rectangle. Then add this handle to the big handle of your GUI so that you are able to have access to it once the callback is called again.
So modify your function like so
function Box(hObject,eventdata,handles)
if (((get(hObject,'Value') == get(hObject,'Max'))))
% Checkbox is checked-take appropriate action
hold on;
handles.rectangleSave=rectangle('Position',[50,50,100,100],'EdgeColor','r');
guidata(handles.output,handles);
else
delete(handles.rectangleSave);
end
If you have never used handles, please have a look here :
http://www.matlabtips.com/on-handles-and-the-door-they-open/
handles.output usually stores the handle to the big interface window as explained here :
http://www.matlabtips.com/guide-me-in-the-guide/