0

I created a GUI named SCADA and I added a boolean variable called StatusData which is a flag that that is set to false when the GUI is launched

handles.StatusData=false;
guidata(handles.output,handles);

I press a button and then a function is called which executes continuously (the function has infinite loop). After some time I press another button which sets the StatusData to true.

handles.StatusData=true;
guidata(handles.output,handles);

Now, I need to access the StatusData from the callback function (the same function I mentioned above), In order to acheieve this I sent the handle as a parameter when I called that function. Now, When the pressed the button that changes the StatusData, the data in the actual handle changes but I cannot access the updated StatusData as the function is already called.

How can I access the updated handle of GUI without sending it as a parameter.

Thanks

podon
  • 61
  • 2
  • 7
  • your description is a bit confusing.. when designing GUIs in MATLAB GUIDE, all callback functions receive the `handles` structure. you can retrieve values from it, as well as set values (just remember to call `guidata` to update them, since `handles` is passed by-value, not as a reference object) – Amro Apr 14 '13 at 06:38

1 Answers1

0

You can just pass in the hObject parameter to your function instead, and retrieve your value when it's needed using guidata; i.e.,

function some_callback(hObject, handles, ...)
    myLoopingFunction(hObject, ...);

function myLoopingFunction(hObject, ...)
    for someVar = something
        % Update handles structure
        handles = guidata(hObject);
    end

Alternatively you could create a handle object and put that in the handles structure; e.g.,

% In a separate file:
classdef uiState < handle
    % Probably should give this class a less general name...
    properties
        StatusData
    end
end

% In your UI file:
function some_callback(hObject, handles, ...)
    handles.state = uiState();
    handles.state.StatusData = false;
    % Now, when you modify handles.StatusData, the version used by
    % myLoopingFunction will also be updated, because they point to
    % the same object!
    myLoopingFunction(handles.state);

function myLoopingFunction(state, ...)
    for someVar = something
        % Now you just have to check state.StatusData
    end

For simple cases, I'd use the first method. For more complicated situations, where several parameters must be kept track of and they interact in complex ways, I would use the second. The second is also useful if you have a large chunk of data in some variable and want to stop it from being copied into every call to the UI.

Personally, for a complex UI I would usually create some application class that keeps track of the user interface (and provides a command-line interface to it) and make sure that was always available to my UI, but that is a bit like using a sledgehammer to open a jam jar in this simple case.

wakjah
  • 4,541
  • 1
  • 18
  • 23