简体   繁体   中英

how to send data from a function out GUI matlab to function GUI.m

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

ButtonDownFcn设置为:

set(im, 'ButtonDownFcn', @(src,evt)clique(src,evt,handles))

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 gcbo 1 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM