When working with Matlab GUIs, is there a way to place a "hint" in an Edit Text box? That is, text that will disappear once the user starts typing? I've used similar functionalities in Android, but I'm not as familiar with other GUIs, so I'm not sure how widespread this functionality is.
This is possible in Matlab, but you have to define a custom MouseClickCallback, which is only accessible using findjobj by Yair Altman (download it from the Matlab File Exchange using the link and save it somewhere on your Matlab path).
Since I like the idea, I have written a function that conveniently takes care of all this. It creates a grayed-out, italic help text that disappears once you click the edit box.
function setInitialHelp(hEditbox,helpText)
%SETINITIALHELP adds a help text to edit boxes that disappears when the box is clicked
%
% SYNOPSIS: setInitialHelp(hEditbox,helpText)
%
% INPUT hEditbox: handle to edit box. The parent figure cannot be docked, the edit box cannot be part of a panel.
% helpText: string that should initially appear as help. Optional. If empty, current string is considered the help.
%
% SEE ALSO uicontrol, findjobj
%
% EXAMPLE
% fh = figure;
% % define uicontrol. Set foregroundColor, fontAngle, before
% % calling setInitialHelp
% hEditbox = uicontrol('style','edit','parent',fh,...
% 'units','normalized','position',[0.3 0.45 0.4 0.15],...
% 'foregroundColor','r');
% setInitialHelp(hEditbox,'click here to edit')
%
% check input
if nargin < 1 || ~ishandle(hEditbox) || ~strcmp(get(hEditbox,'style'),'edit')
error('please supply a valid edit box handle to setInitialHelp')
end
if nargin < 2 || isempty(helpText)
helpText = get(hEditbox,'string');
end
% try to get java handle
jEditbox = findjobj(hEditbox,'nomenu');
if isempty(jEditbox)
error('unable to find java handle. Figure may be docked or edit box may part of panel')
end
% get current settings for everything we'll change
color = get(hEditbox,'foregroundColor');
fontAngle = get(hEditbox,'fontangle');
% define new settings (can be made optional input in the future)
newColor = [0.5 0.5 0.5];
newAngle = 'italic';
% set the help text in the new style
set(hEditbox,'string',helpText,'foregroundColor',newColor,'fontAngle',newAngle)
% add the mouse-click callback
set(jEditbox,'MouseClickedCallback',#(u,v)clearBox());
% define the callback "clearBox" as nested function for convenience
function clearBox
%CLEARBOX clears the current edit box if it contains help text
currentText = get(hEditbox,'string');
currentColor = get(hEditbox,'foregroundColor');
if strcmp(currentText,helpText) && all(currentColor == newColor)
% delete text, reset color/angle
set(hEditbox,'string','','foregroundColor',color,'fontAngle',fontAngle)
else
% this is not help text anymore - don't do anything
end
end % nested function
end % main fcn
Related
I want to be able to use WindowKeyPressFcn when a figure is in zoom mode. This question has been asked recently here Overriding ctrl-z behavior in matlab zoom mode, but I have just made a minimal example to demonstrate the same problem (I would have written a comment on their post, but don't have enough rep yet). Does anyone know what we are missing?
function listenWhileZooming
%% Main problem:
% I want any key press to change the color of the plot, even when in Zoom
% mode. I tried to override the mode manager, but don't see any effect.
%%
%% Create and then hide the GUI as it is being constructed
f = figure('Visible','off','units','normalized','Position',[0.1 0.1 0.5 0.5],'windowkeypressfcn',#colorSwap);
%% Override mode manager
hManager = uigetmodemanager(f);
try
set(hManager.WindowListenerHandles, 'Enable', 'off'); % HG1
catch
[hManager.WindowListenerHandles.Enabled] = deal(false); % HG2
end
set(f, 'WindowKeyPressFcn',#colorSwap);
%% Plot something
plot(1,1,'bo')
%% Make the GUI visible
f.Visible = 'on';
%% Key press callback
function colorSwap(source,eventData)
myLine = findobj(source,'type','line');
if all(myLine.Color == [0 0 1])
plot(1,1,'ro')
else
plot(1,1,'bo')
end
end
end
I know it's late, but this is your missing piece.
I assume (as in your code) that f is the figure handle and f.WindowKeyPressFcn has been set by you.
%% Fix
Button = findall(f, 'Tag', 'Exploration.ZoomIn');
OldClickedCallback = Button.ClickedCallback;
Button.ClickedCallback = #(h, e) FixButton(f, OldClickedCallback, f.WindowKeyPressFcn);
Button = findall(f, 'Tag', 'Exploration.ZoomOut');
OldClickedCallback = Button.ClickedCallback;
Button.ClickedCallback = #(h, e) FixButton(f, OldClickedCallback, f.WindowKeyPressFcn);
function Result = FixButton(Figure, OldCallback, NewCallback)
eval(OldCallback);
hManager = uigetmodemanager(Figure); % HG 2 version
[hManager.WindowListenerHandles.Enabled] = deal(false);
Figure.KeyPressFcn = [];
Figure.WindowKeyPressFcn = NewCallback;
Result = true;
end
After f.WindowKeyPressFcn it set by you, it is reset by the zoom handler. So we highjack both zoom buttons (you can do the same with Pan or Rotate) to first call the original callback and then re-apply your fix. Also, don't forget to delete the KeyPressFcn. It's pretty elegant as you can use the same FixButton for all buttons.
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
I am working in image processing task using matlab,I have made a slider inside a dialog to apply and update gaussian blur filter on an image which is shown in axes
but it can not apply it,it shows that error
Error in ==> MatlabTestProject>name at 392
axes(handles.axes4);
??? Error while evaluating uicontrol Callback
this is my code
function mygui()
out = dialog('WindowStyle', 'normal', 'Name', 'My Dialog','Resize','off');
hSlider = uicontrol('Style','slider','Min',3,'Max',15,'Value',3,'Callback',#gaussian_blur);
%hListener = addlistener(hSlider,'Value','PostSet',#(s,e) disp('hi'));
function gaussian_blur(s,e,handles)
global imag;
slider_value = get(s,'Value');
slider_value=round(slider_value);
%display(slider_value);
%disp('hello')
%create filter
%sliderValueTxt=num2str(slider_value);
%set(handles.kSizeValueText ,'String',sliderValueTxt);
h = fspecial('gaussian', slider_value,0.5);
imag=imfilter(imag,h,'conv');
axes(handles.axes4);
imshow(imag)
% --------------------------------------------------------------------
function gaussianBlur_Callback(hObject, eventdata, handles)
% hObject handle to gaussianBlur (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
gaussian_dialog_Gui();
You're actually really close! All you need to do is include handles as an argument to the function gaussian_blur when defining the slider's listener object callback.
i.e replace this line: (it looks like a test line though)
hListener = addlistener(hSlider,'Value','PostSet',#(s,e) disp('hi'));
with something like this:
hListener = addlistener(hslider,'Value','PostSet',#(s,e) gaussian_blur(handles));
Just to be sure it works I created a test GUI programmatically using an axes and a slider and it works very well! Actually I changed a bit the filter to see an effect on my test image, but it should work as well in your case:
function GaussianSlider()
clear
clc
close all
handles.Image = imread('peppers.png');
handles.fig = figure('Position',[500 500 600 600],'Units','pixels');
handles.axes1 = axes('Units','pixels','Position',[50 100 400 400]);
handles.slider = uicontrol('Style','slider','Position',[50 50 400 20],'Min',3,'Max',15,'Value',3);%// I commented this for the purpose of demonstration. 'Callback',#gaussian_blur(handles));
%// That's the important part: add 'handles' as input argument to
%// gaussian_blur.
handles.Listener = addlistener(handles.slider,'Value','PostSet',#(s,e) gaussian_blur(handles));
imshow(handles.Image,'Parent',handles.axes1);
guidata(handles.fig);
function gaussian_blur(handles)
slider_value = round(get(handles.slider,'Value'));
%// I modified a bit the filter to see the effect
h = fspecial('gaussian',slider_value,slider_value);
handles.Image=imfilter(handles.Image,h,'conv');
axes(handles.axes1);
imshow(handles.Image)
end
end
If we look at 2 screenshots (i.e. 2 different slider positions):
and after moving the slider:
That's it! Hope that helps! If something is unclear please tell me.
Oh and in case you do not know: The actual callback of a slider is only executed when you release the button or press either arrow. As long as you hold the slider and move it, it's only the listener's callback that will be executed.
I'm making a GUI with guide. I have a push button the user clicks and the callback is as folows : (what matters are the tirst two lines really...
function SetParticleRoiSize_Callback(hObject, eventdata, handles)
% hObject handle to SetParticleRoiSize (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles=guidata(hObject);
particleroiSize=imrect;% - draw a rectagle around the particle to get a meausr eof ROI size
roiPoints=getPosition(particleroiSize); %-get tha parameters fo the rectanlge
partX1 = round(roiPoints(1));
partY1 = round(roiPoints(2));
partX2 = round(partX1 + roiPoints(3));
partY2 = round(partY1 + roiPoints(4)); % these are the ROi positions in pixels
roiHeight = round(roiPoints(3)); % - these are just the ROI width and height
roiWidth = round(roiPoints(4));
handles=guidata(hObject); %_ update all the handles...
handles.partX1=partX1;
handles.partX2=partX2;
handles.partY1=partY1;
handles.partY2=partY2;
handles.roicenterX = (partX1 + round(roiPoints(3))/2);
handles.roicenterY= (partY1 + round(roiPoints(4))/2);
handles.roiHeight = roiHeight;
handles.roiWidth = roiWidth;
current_slice = round(get(handles.Image_Slider,'Value'));
handles.current_slice=current_slice;
particleImage=handles.Image_Sequence_Data(partY1:partY2,partX1:partX2,current_slice);
handles.particleImage=particleImage;
set(handles.RoiSizeDisplay,'String',['Particle ROI is ',' ',num2str(roiHeight),' ', ' by ',num2str(roiWidth)] );
guidata(hObject,handles); %- at this point we now have the particle in the roi and the size of the roi stored.
The issue I am having is that when the user clicks the button the first time, nothing happens (i.e. the imrect line seems to not get called at all). When the user clicks the button again then they can draw a rectangle on the image but my program throws out an error
Error in imrect (line 83)
[h_group,draw_api] = imrectAPI(varargin{:});
Error in SemiAutomated_Fionv2p2_5>SetParticleRoiSize_Callback (line 291)
particleroiSize=imrect;% - draw a rectagle around the particle to get a meausr eof ROI size
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in SemiAutomated_Fionv2p2_5 (line 56)
gui_mainfcn(gui_State, varargin{:});
Error in
#(hObject,eventdata)SemiAutomated_Fionv2p2_5('SetParticleRoiSize_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
I guess the error is because there is no imrect created on the first button click.
Can someone please explain to me why this button needs to be clicked twice to work? (and even then it still gives an error...)
thanks...
-j
It looks like this call:
handles=guidata(hObject); %_ update all the handles...
is incorrectly used; you might need to replace it with guidata(hObject,handles) in order to actually update the handles structure. Moreover, since you already call guidata(hObject,handles) at the end of the callback you can delete this line altogether I think.
As it's currently implemented, you:
1) get the handles structure with handles = guidata(hObject);
2) store information in it
particleroiSize=imrect;% - draw a rectagle around the particle to get a meausr eof ROI size
roiPoints=getPosition(particleroiSize); %-get tha parameters fo the rectanlge
partX1 = round(roiPoints(1));
partY1 = round(roiPoints(2));
partX2 = round(partX1 + roiPoints(3));
partY2 = round(partY1 + roiPoints(4)); % these are the ROi positions in pixels
roiHeight = round(roiPoints(3)); % - these are just the ROI width and height
roiWidth = round(roiPoints(4));
3) then call it again (handles = guidata(hObject))`.
Hence on your second call to retrieve the data you have not previously updated it, so I guess Matlab does not like it.
It might not be the specific cause of your error but I think it's worth checking.
I had 3 different axes in my figure. The first version of the program only had 1 axes so it worked fine. After adding the other axes to the GUI (via GUIDE) when I ran just imrect.. I think it was not choosing the axes I wanted..I added a parent handles.correct axes and now it works perfectly!
I would like to know if some of you know of a way to automatically title plots with the name of the object plotted
so that for intance when I plot supermatrix(5:10,:,2:3)
the title (or the legend ..) on the plot says "supermatrix(5:10,:,2:3)"
thanks
Is this for debugging purposes? If not then I suggest you tell us your overall motivation because someone might be able to suggest a more robust method, but this might get you started:
vname = #(x)inputname(1); %//from here: https://www.mathworks.com/matlabcentral/newsreader/view_thread/251347
plot(supermatrix(5:10,:,2:3))
title(vname(supermatrix))
Although to be honest I cannot imagine why this would ever be useful
I think this does what you want and remains pretty flexible:
function h = plotwithtitle( plotstring, varargin )
argstoplot = evalin('caller', ['{', plotstring, '}']);
h = plot( argstoplot{:}, varargin{:} );
title(plotstring);
end
The following examples all work for me:
supermatrix=rand(10,10);
x=1:10;
y=rand(1,10);
plotwithtitle('supermatrix');
plotwithtitle('supermatrix(5:10,:)');
plotwithtitle('x, y');
plotwithtitle('x, y', '--r');
plotwithtitle('1:10', 'r');
plotwithtitle('rand(1,10)');
I've modified the function dfig originally created by F.Moisy for creating docked figures in order to have the command used for plotting show up in the figure name.
The idea is to read the last command in the command history and use that to generate the figure title.
function hh = dfig(varargin)
%DFIG Create docked figure window
% DFIG, by itself, creates a new docked figure window, and returns its
% handle.
%
% DFIG(H) makes H the current figure and docks it. If Figure H does not
% exist, and H is an integer, a new figure is created with handle H.
%
% DFIG(H, name, value,...) reads additional name-value pairs. See
% doc(figure) for available otions.
%
% DFIG will parse the command line input and use the text following dfig
% as figure name. E.g. calling dfig,plot(x(1:3),y(2:2:end)) results in
% the name "plot(x(1:3),y(2:2:end))"
% F. Moisy, moisy_at_fast.u-psud.fr
% Revision: 1.00, Date: 2007/09/11
% Modified (a lot) by Jonas
if nargin==0
h=figure; % create a new figure
else
% call figure with varargin
figure(varargin{:})
h = gcf;
end
if ~any(strcmp('name',varargin(1:2:end)))
% if no name has been supplied: try to use function call
javaHistory=com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory;
if ~isempty(javaHistory)
lastCommand = javaHistory(end).toCharArray';
funCall = regexp(lastCommand,'dfig\s*[,;]\s*(.*)$','tokens','once');
else
funCall = [];
end
if ~isempty(funCall)
if isnumeric(h)
set(h,'Name',[num2str(h),': ',funCall{1}],'NumberTitle','off')
else % HG2
h.Name = sprintf('%i: %s',h.Number,funCall{1});
h.NumberTitle = 'off';
end
end
end
set(h,'WindowStyle','docked'); % dock the figure
if nargout~=0 % returns the handle if requested
hh=h;
end