matlab - Passing variable from event handler -
i grateful help, advice or suggestion. have application control geodetic instrument using synchoronous interface. commands asynchronous nature, e.g. getreflectors
. after command triggered receive many server answers number of available reflectors. have registered com event , associate handler function. far good. can display data coming not know how pass variable main function. tried save variable .mat file or in .txt file , read it. works in matlab not works in compiled .exe aplication (error firing event). disp
command not work in compiled aplication (display nothing). main question is: how pass variables handler main function. there way? global variables? thank filip
edit: adding code demostrate problem... need save reflector name , reflector id user can choose 1 (because there multiple events coming different reflectors).
function pushbutton_getreflectors_callback(hobject, eventdata, handles) ltsync = actxserver ('ltcontrol.ltcommandsync2'); %act server: ltconnect2 ltsync.events() %list of com events ltsync.registerevent({'reflectorsdata' 'reflectorshandler'}) %register event ltsync.getreflectors() %ask instrument reflectors pause(3) %time receive answers end function reflectorshandler(varargin) %handler of event reflectorsdata %var1,var2,reflectorid,reflectorname,var5,surfaceoffset,reflectorstotal,var8,var9 disp('reflector data:'); disp(varargin{3}) %reflector id disp(varargin{4}) %reflector name end
i believe can solve problem passing function handle registerevent
instead of string function name, , creating class allow pass data back. first, class:
classdef reflectorsresponse < handle properties (setaccess = private) reflectors responsecomplete = false; reflectorstotal = nan; end methods function respond(obj, varargin) % create struct each reflector (or define % class, let's keep simple time being) newrefl = struct(); newrefl.id = varargin{3}; newrefl.name = varargin{4}; newrefl.surfaceoffset = varargin{6}; % ... store other properties in struct % store reflector obj.reflectors = [obj.reflectors; newrefl]; % store total reflector count , check completion if isnan(obj.reflectorstotal) obj.reflectorstotal = varargin{7}; end if length(obj.reflectors) == obj.reflectorstotal obj.responsecomplete = true; end end end end
you can use making respond
method handler:
function pushbutton_getreflectors_callback(hobject, eventdata, handles) % create response object , associated handler function handle response = reflectorsresponse(); handlerfun = @(varargin)response.respond(varargin{:}); ltsync = actxserver ('ltcontrol.ltcommandsync2'); %act server: ltconnect2 ltsync.events() %list of com events ltsync.registerevent({'reflectorsdata' handlerfun}) %register event ltsync.getreflectors() %ask instrument reflectors % wait request complete while ~response.responsecomplete pause(0.1); drawnow update; % note: should add timeout loop end % stuff response.reflectors end
this wait until there has been response each of reflectors, number of determined first response. note should add timeout while
loop, otherwise risk waiting infinitely.
if there lots of these asynchronous requests have handle, suggest encapsulating entire request/response sequence (including creation of activex server, setup of event handler, , waiting response(s)) in generalised base class, , deriving specific subclasses each different request.
Comments
Post a Comment