matlab: inputdlg dialog box comes with tick labels - matlab

I have a user input request box basically written as matlab suggests in http://www.mathworks.com/help/matlab/ref/inputdlg.html .
prompt = {'Enter spot number'};
dlg_title = 'Input';
num_lines = 1;
def = {'9'};
answer = inputdlg(prompt,dlg_title,num_lines,def);
handles.count = str2double(answer{1});
However, the dialog box comes with axis labels as the image shows in the link below:
http://imgur.com/yPUYTJb
Edit:
It turns out that the windowsbuttonmotion function is doing the thing. I evaluated it within the function, not by calling a second function. Like this:
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
global loop_counter diameter
pos = get(gca, 'currentpoint'); % get mouse location on figure
x = pos(1,1); y = pos(1,2); % assign locations to x and y
set(handles.lbl_x, 'string', ['x loc:' num2str(x)]); % update text for x loc
set(handles.lbl_y, 'string', ['y loc:' num2str(y)]); % update text for y loc
const = diameter/2;
for i = 1:loop_counter
center(i,:) = getPosition(handles.trap(handles.unselected(i)))+const;
if(((x-center(i,1))^2+(y-center(i,2))^2)<= const^2)
setColor(handles.trap(handles.unselected(i)), 'red');
else
setColor(handles.trap(handles.unselected(i)), 'blue');
end
end
guidata(hObject, handles)
How can I get rid of them?
Thanks.

Related

I would like to record time in seconds each time i am crossing a particular region in Matlab Guide

I would like to track the cursor position within the axes and adds a marker like this for instance(.).
Furthermore, I would like to record and display how much time was spent within a particular region (rectangle, indicating green each time the cursor is within the rectangle.)?
All this done in Guide .
The output in the fig is very different to what I was expecting.
See pictures.
1)The first picture is what I was expecting
2)The second pictures is what I have.
% --- Executes on mouse motion over figure - except title and menu.
function finger_WindowButtonMotionFcn(hObject,~, handles)
% hObject handle to finger (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);
pos = get(hObject, 'currentpoint'); % get mouse location on figure
global x;
global y;
x = pos(1);
y = pos(2); % assign locations to x and y
set(handles.xloc, 'string', ['x loc:' num2str(x)]); % update text for x loc
set(handles.yloc, 'string', ['y loc:' num2str(y)]); % update text for y loc
% Determine if mouse is within the region
p_x = get(handles.region1,'XData');
p_x = p_x([1 2]);
p_y = get(handles.region1,'YData');
p_y = p_y([1 3]);
ax_xl = get(handles.axes1,'XLim');
ax_yl = get(handles.axes1,'YLim');
ax_units = get(handles.axes1,'Units');
if ~strcmp(ax_units,'pixels')
set(handles.axes1,'Units','pixels')
end
ax_pos = get(handles.axes1,'Position'); % axes1 position in pixels
if ~strcmp(ax_units,'pixels')
set(handles.axes1,'Units',ax_units);
end
% convert the patch XData and YData from axes coordinates to figure coordinates in pixels
p_x = (p_x-ax_xl(1))/(ax_xl(2)-ax_xl(1))*ax_pos(3)+ax_pos(1);
p_y = (p_y-ax_yl(1))/(ax_yl(2)-ax_yl(1))*ax_pos(4)+ax_pos(2);
persistent timein;
if isempty(timein)
timein = datetime('now');
end
if x >= p_x(1) && x <= p_x(2) && y >= p_y(1) && y <= p_y(2)
timein = datetime('now');
set(handles.region1,'FaceColor','g');
%writeline(handles.arduinoObj, "4&MOTOR_1_2_3_4&0!");
else
timeInPatch = seconds(datetime('now')-timein);
ax = ancestor(handles.region1, 'axes');
cp = ax.CurrentPoint;
text(ax, 0.52, cp(1,2), sprintf('%.3f sec.', timeInPatch), ...
'HorizontalAlignment','Left', ...
'VerticalAlignment','bottom', ...
'FontSize', 12, ...
'FontWeight', 'bold', ...
'Color', 'b')
set(handles.region1,'FaceColor','r');
%writeline(handles.arduinoObj, "0&MOTOR_1_2_3_4&0!");
end

MATLAB: How to store clicked coordinates (peakpoints) continuosly from time series plot even after panning etc.

I am currently working on a matlab GUI where I need to click unwanted peak points (not to delete them) and store their results in a matrix (continuously). I am using a pushbutton with the following code to collect the points and store results. However, when I clicked the points only the last clicked results stores (not all the clicked points). Also, since it is a continuous plot I am using a PAN pushbutton to move the data. Hence, I would like to do the following:
1)For a pushbutton click (to collect peaks from getpts function) I want to click and collect several points (store and append the values continuously for each click). Also, I want the array to be active even after I use the PAN button to move the plot.
2) I want to create another pushbutton to end the task (asking the user to "do you want to stop collecting the peak points", stop collecting the points and store the entire clicked results in an array)
axes(handles.axes1);
[ptsx1,ptsy1] = getpts(gcf);
idx = knnsearch([t',fbsum],[ptsx1 ptsy1],'k',1)
if evalin('caller', 'exist(''xArray'',''var'')')
xArray = evalin('caller','xArray');
else
xArray = [];
end
xArray = [xArray; idx] %
assignin('caller','xArray',xArray); `% save to base`
save('xArray.mat','xArray');
Sorry, this is my first post and please accept my apology and please clarify if something is not clear. Thanks in advance.
EDIT: Using a GUIDE based GUI Instead
In Matlab command window define your X & Y
>> x = 0:.1:4*pi;
>> y = sin(x);
>> y(10:10:end) = 2; %With some bad points to mark
Then run the GUI, select Start Marking, mark some points Pan around etc.:
>> plotTest(x,y)
After exiting the GUI look at the globla IDX_STORE:
>> global IDX_STORE
>> IDX_STORE
IDX_STORE =
10 30 40
If you want the x , y values marked then it is just
>> markedX = x(IDX_STORE);
>> markedY = y(IDX_STORE);
The GUI is laid out Like this picture.
GUI Code Looks like this:
function varargout = plotTest(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',mfilename,'gui_Singleton',gui_Singleton,'gui_OpeningFcn', #plotTest_OpeningFcn, ...
'gui_OutputFcn', #plotTest_OutputFcn,'gui_LayoutFcn',[] ,'gui_Callback',[]);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before plotTest is made visible.
function plotTest_OpeningFcn(hObject, eventdata, handles, varargin)
global IDX_STORE
IDX_STORE = [];
handles.output = [];
if numel(varargin) ~= 2
handles.closeFigure = true;
else
x = varargin{1}; y =varargin{2};
handles.l = plot(handles.axes1,x,y);
hold(handles.axes1,'on')
handles.axes1.ButtonDownFcn = {#clickCallback,handles};
handles.l.ButtonDownFcn = {#clickCallback,handles};
guidata(hObject, handles);
end
% --- Outputs from this function are returned to the command line.
function varargout = plotTest_OutputFcn(hObject, eventdata, handles)
varargout{1} = [];
if (isfield(handles,'closeFigure') && handles.closeFigure)
errordlg('Nothing Passed in!')
end
% --- Executes on button press in markToggle.
function markToggle_Callback(hObject, eventdata, handles)
switch handles.markToggle.Value
case 1
handles.markToggle.String = 'Stop Marking';
case 0
handles.markToggle.String = 'Start Marking';
end
function clickCallback(hObj,evtData,handles)
global IDX_STORE
if handles.markToggle.Value == 0
return %Do Nothing if toggle not pressed.
end
coordinates = handles.axes1.CurrentPoint(1,1:2); %Get coordinates of mouse click
idx = knnsearch([handles.l.XData' handles.l.YData'],coordinates);%Find closest point in line data
IDX_STORE = unique([IDX_STORE idx]); %Store the index.
mH = findobj(handles.axes1,'tag','markLine');%Add some markers to see what you are doing.
if isempty(mH) %Make the marker plot if it doesn't exist
plot(handles.axes1, handles.l.XData(IDX_STORE),handles.l.YData(IDX_STORE),'rO','tag','markLine')
else%If it does exist then update the markers
mH.XData = handles.l.XData(IDX_STORE); mH.YData = handles.l.YData(IDX_STORE);
end
guidata(hObj,handles); %Save handles structure

How to get value from datacursor in GUI Matlab

How can i get the value of cursor position from datacursor? here's my code
filename = handles.loadDataName;
x=importdata(filename,' ',indexend);
fid = fopen(filename,'r');
A = textscan(fid,'%f%f','Delimiter',' ','headerLines',indexstart);
data = cat(2,A{:});
time = data(:,1);
c1 = data(:,2);
plot(handles.axes_grafik,time,c1)
grid on;
dcm = datacursormode(gcf);
datacursormode on;
set(dcm, 'updatefcn', #myfunction)
function output_txt = myfunction( ~,event_obj)
dataIndex = get(event_obj,'DataIndex');
pos = get(event_obj,'Position');
output_txt = {[ 'Time: ',num2str(pos(1),5)] .....,
['Amplitude: ',num2str(pos(2),5)]};
When i tried to modified function output_txt to get pos(1) and Pos(2) into global variable, i got an error on figure which says 'error in custom datatip string function'
I want to retrieve pos(1) and Pos(2) to display it in editbox. Is there any way to do this? Thanks
[x,y] = ginput
this command will let u click on ur figure as many times as u want until you hit enter then return the xy coords.

MATLAB: How to store clicked coordinates using ButtonDownFcn

Goal: To perform several clicks in one figure, containing an image displayed with imshow and save the coordinates of the "clicked" point(s), to be used in further operations.
Notes: I know about the functions getpts/ginput but I would like to perform this without using them. Is this possible using ButtonDownFcn? (see the following code)
function testClicks
img = ones(300); % image to display
h = imshow(img,'Parent',gca);
set(h,'ButtonDownFcn',{#ax_bdfcn});
function ax_bdfcn(varargin)
a = get(gca,'CurrentPoint');
x = a(1,1);
y = a(1,2);
At this stage the variables x and y only "live" inside ax_bdfcn.
How can I make them available in the testClicks function? Is this possible using ButtonDownFcn? Is this a good approach?
Thanks a lot.
EDIT1:
Thanks for the answer Shai. But I still cannot accomplish what I intended.
function [xArray, yArray] = testClicks()
img = ones(300); % image to display
h = imshow(img,'Parent',gca);
x = [];
y = [];
xArray = [];
yArray = [];
stop = 0;
while stop == 0;
set(h,'ButtonDownFcn',{#ax_bdfcn});
xArray = [xArray x];
yArray = [yArray y];
if length(xArray)>15
stop = 1;
end
end
function ax_bdfcn(varargin)
a = get(gca, 'CurrentPoint');
assignin('caller', 'x', a(1,1) );
assignin('caller', 'y', a(1,2) );
end
end % must have end for nested functions
This code (buggy!) is the closest I can get to what I want (after all the clicking, having an array with the x and y coordinates of the clicked points). I am clealy not understanding the mechanics for the implementation of this task. Any help?
There are several ways
Using nested functions
function testClicks
img = ones(300); % image to display
h = imshow(img,'Parent',gca);
set(h,'ButtonDownFcn',{#ax_bdfcn});
x = []; % define "scope" of x and y
y = [];
% call back as nested function
function ax_bdfcn(varargin)
a = get(gca,'CurrentPoint');
x = a(1,1); % set x and y at caller scope due to "nested"ness of function
y = a(1,2);
end % close nested function
end % must have end for nested functions
Using assignin
function ax_bdfcn(varargin)
a = get(gca, 'CurrentPoint');
assignin('caller', 'x', a(1) );
assignin('caller', 'y', a(2) );
Using 'UserData' property of figure handle
function ax_bdfcn(varargin)
a = get(gca, 'CurrentPoint');
set( gcf, 'UserData', a(1:2) );
'UserData' can be accessed (as long as the figure is alive) using cp = get( gcf, 'UserData');.
EDIT:
An example of a way to "communicate" the clicked locations to 'base' workspace
function ax_bdfcn(varargin)
a = get(gca,'CurrentPoint');
% the hard part - assign points to base
if evalin('base', 'exist(''xArray'',''var'')')
xArray = evalin('base','xArray');
else
xArray = [];
end
xArray = [xArray a(1)]; % add the point
assignin('base','xArray',xArray); % save to base
% do the same for yArray
After calling testClicks there are NO xArray or yArray variables in the workspace (at least there shouldn't). After the first click these two variables will "miraculously" be created. After every other click these two arrays will increase their size until you close the figure.

Matlab code to draw a tangent to a curve

I need to draw a tangent to a curve at a particular point (say the point is chosen by the user). I have written a code that allows the user to manually pick up two points and then a line is drawn between them. But I would like to automate the process. Can someone please suggest any algorithms/already implemented matlab codes to do so?
Try the function below. Of course, it needs lots of tweaking to apply to your case, but I think this is roughtly what you want.
function test
hh = figure(1); clf, hold on
grid on
x = 0:0.01:2*pi;
f = #(x) sin(x);
fprime = #(x) cos(x);
plot(x, f(x), 'r')
axis tight
D = [];
L = [];
set(hh, ...
'WindowButtonMotionFcn', #mouseMove,...
'WindowButtonDownFcn', #mouseClick);
function mouseMove(varargin)
coords = get(gca, 'currentpoint');
xC = coords(1);
if ishandle(D)
delete(D); end
D = plot(xC, f(xC), 'ko');
end
function mouseClick(obj, varargin)
switch get(obj, 'selectiontype')
% actions for left mouse button
case 'normal'
coords = get(gca, 'currentpoint');
xC = coords(1);
yC = f(xC);
a = fprime(xC);
b = yC-a*xC;
if ishandle(L)
delete(L); end
L = line([0; 2*pi], [b; a*2*pi+b]);
case 'alt'
% actions for right mouse button
case 'extend'
% actions for middle mouse button
case 'open'
% actions for double click
otherwise
% actions for some other X-mouse-whatever button
end
end
end