MATLAB GUI Radio Button variable storage - matlab

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.

Related

How to save a value generated in middle of a MATLAB simulink operation

I have MATLAB Simulink model running successfully. In this model, a function runs code to do the operation. The function code generates a value during the operation. I want to save this value and use at successive operations.
In above screen shot, Icur_in and Icur_ou are input and output of the function. In fact both refer to same value. I am using memory function to hold the value for next operation. Up to this point is fine. But the value keeps on changing.
My present code is:
Function [Icur_ou] = fun(Icur_in)
Icur_ou = Icur_in;
if somecondition
Icur_ou = I_s;
end
end
I am not 100% sure about what your problem is, since you state: "Up to this point is fine." And also your model looks fine to me, but nevertheless I found this link, which seems to be about the same problem. Besides the suggested solutions, I also liked the first two comments about persistent variables, which I think could maybe also help you depending on the sample time of your model (see comments in the link).
In this case, you would not need the memory block, but instead you'd have to make your Icur_ variable persistent, so that it stays in memory between function calls. Similar to this:
Function [Icur_ou] = fun(Icur_in)
persistent Icur_ou = Icur_in;
if somecondition
Icur_ou = I_s;
end
end

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);

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.

matlab: how to set dynamically invisible what is visible

I have a simple question about matlab using interface.
I found that if I first declare an object (for example a table with the command UI table) NOT visible and after I make it visible, it works i.e. I see effectively in the figure the modification.
On the contrary, and here it's my problem, If I first declare an object visible and after I make it NOT visible, it doesn't work i.e. I not obtain the invisibility of the object.
Making an example:
figure;
h_tabell=uitable(gcf,'vis','off','data',randn(3));
h_tabell=uitable(gcf,'vis','on','data',randn(3)); %
here I see that the table is now visible
%Now I want make it that table invisible again, with
set(h_tabell,'vis','off') % here I see that the table already is visible
I need it, because in my program, the object is visible and if the user needs, I want to to set invisible the same object.
Anybody can help me?
You should only use the Matlab function set to change the properties of your object.
Here you make two calls to uitable, so here is what really happen if you break it down:
the first call create an invisible table
the second call create another table (overwriting the variable h_tabell with the new handle) but this time visible
You can verify this by checking that your figure now have two children:
children=get(gcf,'children');
Now if you try to change the visible property of the object referenced by the handle h_tabell, it will only apply to the second table.
The following piece of code works as expected and create only one table:
figure;
h_tabell=uitable(gcf,'visible','off','data',randn(3));
% Switch the table to visible
set(h_tabell,'visible','on');
% Switch it back to invisible
set(h_tabell,'visible','off');