Matlab update variable outside the timer - matlab

I am struggling trying to find a simple solution to this Timer issue.
I would like the timer to update the entire workspace so I could re-use the updated value to be display on a GUI.
Here is the Matlab timer,
handles.server_timer = timer('TimerFcn',#timer_server,'UserData',handles);
start(handles.server_timer);
it calls the function timer_server.m (bellow) :
function timer_server(obj,~)
handles=obj.UserData; % Get User Timer input data
% GET DATA & GENERATE/UPDATE OBJECT
[handles.SERVER,handles.CLIENT,handles.OBJ] = Lib_Zig.zigREAD(handles.SERVER,handles.CLIENT,handles.OBJ );
obj.UserData=handles; % Set User Timer output data
end
Currently the handles is being updated inside the TIMER but not in the WORKSPACE so the updated HANDLES is not accessible/saved within the rest of the GUI.
I could use GLOBAL VARIABLE but from what I hear it is not very efficient and could slow down the program.
I could also update the GUI from inside the timer but I think it would make more sense to have 2 timers. One to handle the Network data flow and an other to handle the GUI interface.
Would anybody have an idea on how to get the timer to update the HANDLES within the entire WORKSPACE at every period ?
Thank you for your help :-)

I don't use handles and guidata in my GUI's so I might be wrong here - but I suspect you are missing a call to guidata:
guidata ( uiObject, handles )
This is needed to make sure that the handles is updated in the core figure -> so that all callbacks etc.. can pick it up.

Related

In Matlab, how to call GUI callback functions without the GUI?

I am not a GUI programmer, as will become obvious. If anything, I'm trying to unmake a GUI. I am using a program called art (found here, if it's useful to see) that generates figures and variables that I would like to save. You can call art from a batch script and make it read a config file for its inputs, which is what I'm doing, but you have to manually generate and save much of its output (variables and figures) in the GUI. I'd love to automate this process, but I am really struggling.
I think the core of my issue would be solved if I knew how to force the callback functions to be called. For instance, there is a function in art showCorr_Callback(hObject, eventdata, handles) (which is governed by a radio button in the GUI). It has a test condition to execute:
if (get(handles.showCorr,'Value') == get(handles.showCorr,'Max'))
I've tried inserting
mx = get(handles.showCorr,'Max'))
setappdata(handles.showCorr,'Value', mx)
into a function I know executes, the opening function, function art_OpeningFcn(hObject, eventdata, handles, varargin). That doesn't seem to have any effect. If I knew how to make the callback functions execute, perhaps I could insert the code that saves the figure into the functions. Somewhere in Matlab's GUI scripts there must be something that is continually testing for a change in state in the GUI. Where is that thing? How can I fool it into thinking a radio button has been pressed?
Thanks for your help, and please let me know if I need to provide more information.
First of all, if you want to set the Value of handles.showCorr, you won't use setappdata as that just stores arbitrary data (by key/value pair) into the graphics object. You actually want to set the Value property.
set(handles.showCorr, 'Value', get(handles.showCorr, 'Max'))
This should trigger any callbacks that are assigned to handles.showCorr.
If, for some reason this doesn't trigger the callback, you can always trigger it manually. If you already know the callback, you can call it explicitly.
showCorr_Callback(hObject, eventdata, handles);

Updating all components of a Matlab gui (created with guide)

My Matlab GUI is a form with many text fields that are initially populated using the same data struct. Each text field has a callback and a create function, where the text field is assigned the value of the given struct. However, at some later point I would like to repopulate the form using a different struct, as an event triggered by pressing a push button. The code looks approximately like:
h = MyFigure;
global mystruct
mystruct = somevalues;
handles = guidata(h);
set( handles.textfield1, 'String', mystruct.value1 )
...
set( handles.textfieldN, 'String', mystruct.valueN )
However, if I could make Matlab call all these callbacks recursively (like a "validate tree" function), I wouldn't have to call "set" for each text field. I have tried refresh(h) and drawnow(), with no luck.
Now my question is whether or not there is such a function built into the matlab guide framework?
When you set a property of a handle like set(h,'String',str), the value of str is copied. It is not a reference to that variable that can be updated automatically. Your best bet is to make a subroutine called updateText or something like that, put all of the set statements in it, and call it when needed.
Calling guidata(hObject, handles); is only for updating the GUI with modifications to handles. You may need this elsewhere, but for the job of updating properties of certain handle graphics objects, it is not really used.
One possibility is to create a timer to update the text fields on a regular basis. In your GUI's opening function, create a timer that defines an update function to run periodically:
T = timer('Period',1,'StartDelay',0.5,'TimerFcn', ...
{#updateTextBoxes,handles},'ExecutionMode','FixedRate');
start(T)
The update function would look like:
function updateTextBoxes(hTimerObj, timerEvent, handles)
global mystruct
% get mystruct data however you do it...
% maybe also get handles via handles=guidata(hTimerObj); instead of input
set( handles.textfield1, 'String', mystruct.value1 )
...
set( handles.textfieldN, 'String', mystruct.valueN )
EDIT: Do NOT forget to delete the timer (delete(T)) or stop it before you quit the GUI or do clear T, otherwise it will just keep running and you will have to quit MATLAB... No, I did not just do this myself!
You need to update the handles structure with this :
% Update handles structure
guidata(hObject, handles);

GUIDE in MATLAB

I am trying to build a program in MATLAB and I am using edit boxes but I want the value that the user will enter to be used later on from another function. So should I use the global variables or is there another way?
I tried to define the global variables outside the function but is not working.
I tried to define it inside the function and then call it from another function but it says that it is undefined. Is there a way that I can do that?
I am just using
function edit1_Callback(hObject, eventdata, handles)
str2double (get (hObject,'String'));
Thanks!! :)
If you want to store data within a Matlab-GUI, you can use the handles-structure like this:
handles.myVar=123;
%after this dont forget to save it (yes, this is a bit annoying):
guidata(hObject, handles);
later on, within another callback for example, you can find this data within the handles-struct:
handles.myVar

Reading GUI Data from a Matlab Script File

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
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.

Avoid interruption of callback functions in Matlab GUI

I have a Matlab GUI that needs a high time to execute some callback functions. Besides, these functions include the following code:
drawnow('expose');
pause(handles.data.delay);
I want to avoid that those callback executions get interrupted in order to avoid data inconsistency if the user presses other buttons. Thus, I modify the figure settings as:
set(handles.figure, 'BusyAction','cancel', 'Interruptible','off');
However, the callbacks are still interrupted. How can I avoid it?
Note: I think that the problem is that I need to propagate the 'BusyAction' and 'Interruptible' values to all the controls in my GUI, is there any way to do it automatically? Like, for example, modifying the default value before generating the GUI.
The fastest and cleanest way to propagate any property to all UI objects is with findobj:
set(findobj('Type','uicontrol'), 'BusyAction','cancel', 'Interruptible','off');