I have a simple GUIDE with 2 push buttons:
Button #1 plots a sine graph.
Button #2 adds a "movable" vertical line to the graph.
Everything works fine and I can drag vertical line as desired.
What I'm trying to do now is to record x-position of vertical line (x_history) as I move it and save this x_history to the handles (via guidata) such that if I stop moving the line (mouse-up) and then resume moving the line (mouse-down and mouse-move) I can restore previous x_history from handles (via guidata).
The problem I'm facing is that every time go mouse-up the handles resets(!) the x_history field (removes the field), so I loose the previous record of x_history when I resume mouse-down and move the line.
What am I missing here?
PS: I found these posts relevant but not exactly applicable to my case given the presence of nested functions.
Post 1
Post 2
Post 3
Thanks,
Alborz
Here is the code:
function varargout = plot_test(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #plot_vertline_handles_OpeningFcn, ...
'gui_OutputFcn', #plot_vertline_handles_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
function plot_vertline_handles_OpeningFcn(hObject,~,handles,varargin)
handles.output = hObject;
guidata(hObject, handles);
end
function varargout = plot_vertline_handles_OutputFcn(~,~,handles)
varargout{1} = handles.output;
end
%% Vertline Callback
function pushbutton_vertline_Callback(hObject,~,handles) %#ok<*DEFNU>
axis_h = handles.axis_h;
vertline_h = line('parent',axis_h,'xdata',[1 1],'ydata',[-1 1],...
'ButtonDownFcn', #mouseDown );
handles.vertline_h = vertline_h;
guidata(hObject,handles)
function mouseDown(~,~)
mousedown = true;
handles.mousedown = mousedown;
guidata(hObject,handles)
end
end
%% Plot Callback
function pushbutton_plot_Callback(hObject,~,handles)
clc;
open_figs_h = get(0,'children');
close(open_figs_h(2:end));
x = -pi:0.01:pi;
y = sin(x);
fig_h = figure('units','normalized','outerposition',[0.2 0.2 .5 .5],...
'WindowButtonMotionFcn', #MouseMove, 'WindowButtonUpFcn', #MouseUp );
axis_h = axes('parent',fig_h,'position',[0.1 0.1 .8 .8]);
line('parent',axis_h,'xdata',x,'ydata',y);
handles.axis_h = axis_h;
guidata(hObject,handles);
function MouseUp(~,~)
handles = guidata(hObject);
handles.mousedown = 0;
guidata(hObject,handles);
end
function MouseMove(~,~)
try handles = guidata(hObject);catch, end
if isfield(handles,'mousedown')
mousedown = handles.mousedown;
else
mousedown = 0;
end
if mousedown
x_current = get(axis_h,'CurrentPoint' );
vertline_h = handles.vertline_h;
set (vertline_h, 'XData',[x_current(1,1) x_current(1,1)] );
if isfield(handles,'x_history')
disp('Field "x_history" Exists in handles')
handles.x_history = [handles.x_history x_current(1,1)]
else
disp('Field "x_history" Does Not Exist in handles')
handles.x_history = x_current(1,1)
end
guidata(hObject,handles);
end
end
end
With Jan Simon's guideline, I figured what was missing in the code:
In the mouseDown callback (the nested function in pushbutton_vertline_Callback), I was missing to get the updated copy of handles. So, I needed to add this line to that nested function:
handles = guidata(hObject);
Related
I am trying to send data from my arduino to matlab and use the GUI. I want to read continously the data even when no button is pressed. In order to so, i have to use the fscanf function but i don't know where to put it. There is definitely a while loop that waits the events(such as a pushed button) in which this function should be placed. I am just a beginner so this might be a silly question for you.
Thank you in advance!
function varargout = UltraPlot(varargin)
global a;
global k;
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #UltraPlot_OpeningFcn, ...
'gui_OutputFcn', #UltraPlot_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
disp('Ultraplot');
function UltraPlot_OpeningFcn(hObject, eventdata, handles, varargin)
global a;
global k;
a = serial('COM3');
fopen(a);
handles.output = hObject;
guidata(hObject, handles);
disp('Opening');
function varargout = UltraPlot_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
global a;
global k;
disp('varargout');
function Start_Callback(hObject, eventdata, handles)
global a;
global k;
fprintf(a,'%d',1);
disp('Button pressed');
You have to set Matlab to wait for any data from Arduino in a while loop, check this sample code :
clear;clc;
S=serial('com18'); % Create an S Object
data=0;
set(S,'inputbuffersize',4096,'timeout',20); % Set serial communication parameter
fopen(S); % Open serial communcation
while (1)
if s.bytesavailable>0 % If data from Arduino is available
data=fscanf(S);
data = str2num(data);
% Do whatever you want with data here...
end
data=0;
end
I have applied Canny edge detection on am image and now want to crop a part of it for further procession. I have created 4 axes with the tags axes1, axes2, axes3 and axes4. I want to display the image first in axes1, then the edge detected again in axes1. Finally the cropped image is to be displayed in axes3 and the image after removal of holes in axes4.
I am including a sample code which recreates the problem:
function varargout = samp_GUI(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, 'gui_Singleton', gui_Singleton, 'gui_OpeningFcn', #samp_GUI_OpeningFcn, 'gui_OutputFcn', #samp_GUI_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 samp_GUI is made visible.
function samp_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = samp_GUI_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in browse.
function browse_Callback(hObject, eventdata, handles)
[filename pathname] = uigetfile({'*.jpg';'*.bmp'},'File Selector');
handles.myImage = strcat(pathname, filename);
axes(handles.axes1);
imshow(handles.myImage)
im=imread(handles.myImage);
bw3=im2bw(im,0.3); %threshold value taken as 0.3
BW3=edge(bw3,'canny');
edge_im = BW3;
axes(handles.axes1);
imshow(BW3);
h=imfreehand(handles.axes1);
M=~h.createMask();
I(M) = 0;
axes(handles.axes2);
imshow(I);title('Cropped Image')
I = bwareaopen(I, 50);
axes(handles.axes3);
imshow(I),title('Removed Holes');
% save the updated handles object
guidata(hObject,handles);
When i execute the code in a .m file, it runs perfectly with different images opening in different windows but when the same code is run for the GUI, blank images are problems. I do not seem understand where the problem lies.
I think that you're suffering from some form of copy/paste issue. If you look at the line in your code where I(M) = 0 this doesn't really make sense (given your example) since I is not defined.
MATLAB will not throw any errors at this point though, because it's perfectly valid to create a matrix in this way; however, doing so actually produces a row vector rather than the image you're wanting.
>> M = logical(eye(100));
>> size(M)
ans =
100 100
>> clear I
>> I(M) = 0;
>> size(I)
ans =
1 10000
I imagine what you wanted to do was to apply the mask to your image (im) in which case you should get what you want. I have provided an example below (not sure what you're trying to display in axes1).
load mri
img = double(D(:,:,12));
im = img ./ max(img(:));
figure
handles.axes1 = subplot(2,2,1);
handles.axes2 = subplot(2,2,2);
handles.axes3 = subplot(2,2,3);
handles.axes4 = subplot(2,2,4);
bw3=im2bw(im,0.3); %threshold value taken as 0.3
BW3=edge(bw3,'canny');
edge_im = BW3;
axes(handles.axes1);
imshow(BW3);
h=imfreehand(handles.axes1);
M=~h.createMask();
% Assign im to I to ensure proper cropping.
I = im;
I(M) = 0;
axes(handles.axes3);
imshow(I);title('Cropped Image')
M_noholes = bwareaopen(M, 50);
I(M_noholes) = 0;
axes(handles.axes4);
imshow(I),title('Removed Holes');
If you provide more information it will be easier to help you figure out what you need.
I would be very grateful If anyone could help me to solve this problem.
Initially an Image to be loaded to the MATLAB. Image must contain an oval or circular shape object. I choose and draw the circle/ellipse there.
I have completed the above mentioned job. I have attached the MATLAB code and for figure just get full mooon image or egg for ellipse.
Now I want to calculate the area of that ellipse. Anybody there to sort me out from this situation.
Thank you
Here is the Code
function varargout = stack_overflow(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #stack_overflow_OpeningFcn, ...
'gui_OutputFcn', #stack_overflow_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
%%
function stack_overflow_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
%%
function varargout = stack_overflow_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
%%
function load_image_Callback(hObject, eventdata, handles)
axes(handles.view);
global im
[path, user_cancel] = imgetfile();
if user_cancel
msgbox(sprintf('Error !!! \n Please upload the image'),'Error','Error');
return
end
im = imread(path);
imshow(im);
%%
function draw_circle_Callback(hObject, eventdata, handles)
axes(handles.view);
global temp;
global fcn;
temp = imellipse(gca, []);
fcn = makeConstrainToRectFcn('imellipse',get(gca,'XLim'),get(gca,'YLim'));
setPositionConstraintFcn(temp,fcn);
%%
function result_Callback(hObject, eventdata, handles)
%%
function result_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
You could use the createMask method of the imroi class to create a logical mask with the ellipse just drawn, and then sum all the pixels whose value is 1 (or 0...) in order to get the area. You can add something like this in your draw_circle_Callback:
Simple example which you can modify to adapt with your current code:
%// Create dummy image with logical false values.
Im = false(500,500);
imshow(Im);
hold on
%// Draw ellipse
hEllipse = imellipse
%// Create a logical mask
roi = createMask(hEllipse);
%// Sum the values equal to 1;
Area = sum(roi(:))
How can I pass values (x_locate, y_locate) for a static text in my GUI?
Because the function is out functions generated by the GUI. I'm not achieving to configure the set() function.
I believe it is using handles, but I've tried everything and failed.
to simplify I rewrote the code:
![enter image description here][1]
The "Locations.fig" have: 1 axes, 1 pushbutton and 2 static text.
ctrl+C
function varargout = Locations(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #Locations_OpeningFcn, ...
'gui_OutputFcn', #Locations_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
function Locations_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = Locations_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
cla(handles.axes1,'reset');
axes(handles.axes1);
image = imread('eight.tif');
im = imagesc(image);
set(im,'ButtonDownFcn', #clique);
function clique (gcbo,eventdata,handles)
pos = get(gca, 'currentpoint');
x_locate = round(pos(1))
y_locate = round(pos(3)) % until here working!!!
set(handles.text1, 'string', ['x loc:' num2str(x_locate)]); %don´t working
set(handles.text2, 'string', ['y loc:' num2str(y_locate)]); %don´t working
Only callbacks set through GUIDE (or some other custom means) receive the extra handles argument. Otherwise, you'll need to retrieve the handles structure manually within the clique function:
handles = guidata(<object>);
The question of what <object> is depends on your GUI setup. If im is a child of the GUI figure, then gcbo1 will do. If it's in a separate figure, then you'll need to get a handle to the GUI figure. Using findobj to either enumerate all figures or search for some specific property of your GUI figure is the straightforward way to do it.
For example, all Handle Graphics objects have a 'Tag' property that you are free to use, which would be helpful in this case. In GUIDE, set the Tag property on your GUI figure to 'my GUI', then you can retrieve the data from anywhere like so:
hfig = findobj('Tag','my GUI');
handles = guidata(hfig);
[1] Incidentally, giving variables the same name as built-in functions isn't a great idea
Set the ButtonDownFcn to be:
set(im, 'ButtonDownFcn', #(src,evt)clique(src,evt,handles))
The Notlikethat resolved the problem! Thanks!!!
so:
x_locate = round(pos(1));
y_locate = round(pos(3)); % until here working!!!
hfig1 = findobj('Tag','text1');
handles = guidata(hfig1);
hfig2 = findobj('tag','text2');
handles = guidata(hfig2);
set(handles.text1, 'string', ['x loc:' num2str(x_locate)]);
set(handles.text2, 'string', ['y loc:' num2str(y_locate)]);
finalized
I want to create a gui program with matlab but i want to use multiple gui. for example I have the main gui function and I want to get data from another gui with edit textbox. In the example below, I want to return the p variable to the main gui.
THE MAIN GUI:
function varargout = FoProgram(varargin)
gui_Singleton = 0;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #FoProgram_OpeningFcn, ...
'gui_OutputFcn', #FoProgram_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
function FoProgram_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = FoProgram_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function labor_2_Callback(hObject, eventdata, handles)
function fel1_Callback(hObject, eventdata, handles)
cla reset;
clc;
clear all;
n = guzu() %Here I call the second Gui function with edit textbox
uiwait(gcf);
x=linspace(-3*pi,3*pi,1000);
y=sin(x);
plot(x,y,'k','LineWidth',4)
sz='ymcrgbkymcrgbkymcrgbkymcrgbk';
hold on
title('Sin(x) Taylor sora')
%n = str2num(N);
f=zeros(size(x));
for i=1:n
t=(-1)^(i-1)*x.^(2*i-1)/factorial(2*i-1);
f=f+t;
plot(x,f,sz(i),'LineWidth',2)
axis([-10 10 -10 10])
pause(1.5)
hold on
n=n+1;
end
function exit_Callback(hObject, eventdata, handles)
close
THE SECOND GUI
function varargout = guzu(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #guzu_OpeningFcn, ...
'gui_OutputFcn', #guzu_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
function guzu_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = guzu_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function edit1_Callback(hObject, eventdata, handles)
p = str2double(get(hObject,'String')) %I want to return this 'p' to the main gui
close
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
This kind of simple user input is easy to accomplish using the inputdlg function, without the need to create a separate GUI - here's an example
inputTitle = 'Input Required';
inputPrompt = 'Enter a value for ''p'':';
userInput = inputdlg(inputTitle, inputPrompt);
if isempty(userInput)
% User cancelled
return;
else
p = userInput{1}; % userInput is a cell array
% Do something with p
end
It may be that the example you provided is a very minimal version of your end-goal for the second UI, so this may not be appropriate (though note that the inputdlg function is capable of some more complex behaviour - see the documentation). If you wish to continue using your separate UI, then you have to make a couple of modifications
%%% Add this line to the end of OpeningFcn
uiwait;
%%% Modify OutputFcn to have the following code:
varargout{1} = str2double(get(handles.edit1, 'String'));
% The figure can be deleted now
% NOTE: You have to change this to the name of your figure,
% if it's not called figure1
delete(handles.figure1);
%%% Add the CloseRequestFcn callback and put this code in it
if isequal(get(hObject, 'waitstatus'), 'waiting')
% The GUI is still in UIWAIT, us UIRESUME
uiresume(hObject);
else
% The GUI is no longer waiting, just close it
delete(hObject);
end
Most of the above is directly copied from here. You can also remove the call to uiwait from FoProgram.