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.
Related
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);
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.
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);
I am an engineering student, fairly new to MATLAB. I have created a GUI for a class that calculates voltages and amperages of a specified circuit. I wish to display the amperages as (A) and (mA). The program currently calculates the data and displays it in static text boxes. I am using a button group with two radio buttons inside, working exclusively. I have used selectionChangeFcn in the following manner to control the buttons.
function group_SelectionChangeFcn(hObject, eventdata, handles)
switch get(eventdata.NewValue,'Tag') % Get Tag of selected object.
case 'radiobutton1'
var=1;
set(handles.text1, 'String', '(A)');
case 'radiobutton2'
var=1000;
set(handles.text1, 'String', '(mA)');
otherwise
end
Choosing one button or the other changes text in the static text boxes, and assigns a value to a variable. The bulk of the programming code is executed under a pushbutton. All of the variables are contained in this code, and are filled from edit boxes. Everything else works great so far. Within the cases I have (A) or (mA) outputted to a static text box, and you can see that operating the buttons does in fact display different values.
My problem is this; I wish to use variable var in the code to multiply my answer data so that it reads in either A or mA. Like this;
set (handles.text36,'string',num2str(ir1*var,'%20.3f'))
I cannot get this to work, however, the error says that var is undefined. It seems to do this in all circumstances. I have experimented with moving the code to different locations, but I cannot get it to work. Any help or ideas would be appreciated.
From what it looks like var is defined within the switch statement, thus it’s scope is just inside of the switch. You should define var outside of the switch to make its scope open to the function.
Also, when writing code in stack overflow, have a line break between your paragraph and indent the code 4 spaces and it’ll be formatted like a block of code, see below. Finally, include line breaks so one can determine easily where your lines of code are.
switch (...)
methods
end
You might profit from reading these articles:
http://www.mathworks.de/de/help/matlab/matlab_prog/base-and-function-workspaces.html
http://www.mathworks.de/de/help/matlab/creating_guis/ways-to-manage-data-in-a-guide-gui.html#f5-998711
You'll have to store var somewhere where it's accessible from other gui-functions.
One example:
Change & store var in your first function, e.g. as part of the handles structure:
% set var as a field on handles
handles.var = 1000;
% save the guidata - don't miss this!
guidata(hObject, handles);
In your second function, that's supposed to use var you can now take var from the handles structure:
set(handles.text36,'string',num2str(ir1*handles.var,'%20.3f'))
Based on the idea that your set line is not in the same function as the other methods, you must have the var either defined globally, sent through the function calls, or saved to handles or some other object. Here is a method of saving data that can be accessed from any function.
First, save the handle to the main gui, to the root address. Call this in the Main_OpeningFcn of your program.
setappdata(0, ‘mainGUI’, gcf);
Then, in your code call
mainGUI = getappdata(0, ‘mainGUI’);
var = someValue;
setappdata(mainGUI, ‘var’, var);
This saves the var variable inside of mainGUI. Now you have access to var from wherever you want just by calling getappdata. You do have to get mainGUI first, but that is only one line. Doug Hull has a video on MatLabCentral. I can find the link for you later that discusses this in more detail. One benefit with this method is that you don’t have to keep passing the handle structure around if you don’t need it.
I save and load gui data as described in the manual using
function readImage(filename, hObject, handles)
handles.image.data = imageRGBNoEdge;
guidata(hObject,handles);
and
function createHistogram(handles)
imageRGB = handles.image.data;
which are both called directly after the other
readImage(imageFile,hObject,handles);
createHistogram(handles);
However in he second function handles.image is unkown.
??? Reference to non-existent field 'image'.
Error in ==> ui_histogram>createHistogram at 252
imageRGB = handles.image.data;
But if I call the function a second time it is known?
Despite its name, handles is a structure and not a handle class so changing it within readImage does not propagate the change to the calling function.
You could either change readImage to be
function handles = readImage(filename, hObject, handles)
and call it with
handles = readImage(imageFile,hObject,handles);
or add a call to guidata
handles = guidata(hObject);
just before your calll to createHistogram.
I'm working off the assumption that imageRGBNoEdge is already defined at this point.
By briefly looking over the guidata function documentation it looks like that could be part of the issue here. When you try to load the data within createHistogram you need to be using something like this: imageRGB = guidata(hObject) because that is what tells guidata what data to get for you and you've already saved the data to that object handle (hObject)
Also, I can't really comment much more because it looks like there is also a fair bit of missing code for each of those functions. Hopefully this will help get you going in the right direction though!