Global Variables in GUI

10 次查看(过去 30 天)
Adam Glick
Adam Glick 2015-10-12
Hi. I'm trying to save the value of a global variable in my GUI to reuse it later in a different function. I'm using the global function to do this. The problem I'm running into is when I test my function to see that the value is being saved after the function is done does not work when I run my GUI in its entirety, but it DOES work when I run it in step form. I'm declaring the global variable outside of the function and then within the function as well.
  2 个评论
Geoff Hayes
Geoff Hayes 2015-10-12
Adam - please describe the workflow of your application. Does your GUI perform some calculation and create a number (or object) that you wish to later pass to some function that is called from another part of the GUI. Or do you have a separate script that you run after the GUI has done it job (whatever that is). Please also describe what you mean by it does work when I run it in step form. What are you running and what are the steps?
Adam Glick
Adam Glick 2015-10-12
Here is the code in its most basic form:
global selectedItem;
hspopup0 = uicontrol('Style','popup','BackgroundColor','white','position',[20,270,100,40],'String',{'Modality';'PBS';'DS'},'Callback',@modality);
function modality(hObject,eventdata,handles)
selectedIndex = get(hspopup0, 'Value');
allItems = get(hspopup0,'String'); % Cell array of all items in the popup
selectedItem = allItems{selectedIndex};
end
if strcmp(selectedItem ,'PBS') == 1
20*20
end
If the user selects 'PBS' from the popup menu the program should calculate 20*20 but it's not doing that.

请先登录,再进行评论。

回答(3 个)

Image Analyst
Image Analyst 2015-10-12
Exactly what does "in my GUI" and "outside the function" mean? Are you using GUIDE? If so, everything is a function. Every function that you declare the variable global in will be able to see and modify the variable. Functions that do not declare it global will have no knowledge that that variable exists. I can really say much more unless we get the file(s) or at least snippets of it.

Image Analyst
Image Analyst 2015-10-12
In modality, you don't have selectedItem declared as global. That's probably the main problem you're having.
Another major problem is that in modality(), you have not passed in hspopup0, so the get() function will throw an error.
And in
if strcmp(selectedItem ,'PBS') == 1
20*20
end
You're not assigning the 20*20 to any variable. And strcmp() gives true or false, so you don't need to compare it to a double or integer with "==1".
Also, what function does this occur in:
global selectedItem;
hspopup0 = uicontrol('Style','popup','BackgroundColor','white','position',[20,270,100,40],'String',{'Modality';'PBS';'DS'},'Callback',@modality);
ALso, what function does it occur in? It is outside the modality() function, but where is it?
  2 个评论
Adam Glick
Adam Glick 2015-10-12
So if I declare selectedItem as global in the modality function, I get the error:
Error: File: NeutronFluence.m Line: 53 Column: 8 The GLOBAL or PERSISTENT declaration of "selectedItem" appears in a nested function, but should be in the outermost function where it is used.
I've been trying to get rid of that but the only way I can is by removing "global" from inside the modality function.
Image Analyst
Image Analyst 2015-10-12
The structure of your program is all messed up. And I know you thought it made it easier to debug by giving us your program "in its most basic form" but doing that may have introduced other errors.
1. I don't know why you have nested functions. Was that intentional on your part? Or is that just an artifact of your copying and pasting?
2. How many separate m-files are you using? Just one, or two or more? If so, what functions are in each one? Which are composed of functions and possibly nested functions, and which are strictly scripts? (Note, you cannot mix scripts and functions in the same file, in case you were trying to do that.)

请先登录,再进行评论。


Geoff Hayes
Geoff Hayes 2015-10-12
Adam - since you are creating your GUI programmatically, try nesting your callback within the "parent" function that creates the GUI. That way, any local variable declared in the parent function is accessible by the nested function(s) and you can do away with global variables. For example, save the following to a file named as myGuiTest.m and run it
function myGuiTest
close all;
selectedItem = 0;
hspopup0 = uicontrol('Style','popup','BackgroundColor','white','position',[20,270,100,40],'String',{'Modality';'PBS';'DS'},'Callback',@modality);
function modality(hObject,eventdata)
selectedIndex = get(hspopup0, 'Value');
allItems = get(hspopup0,'String');
selectedItem = allItems{selectedIndex};
if strcmp(selectedItem ,'PBS')
fprintf('PBS has been selected!!!\n');
end
end
end
I think that part of the problem in your code is the assumption that once the callback has fired, then the subsequent code (that which checks to see if PBS has been selected) will fire (which is not the case).
Presumably, any other callback within in your GUI could then access the selectedItem if that callback is nested within the parent myGuiTest function.
  1 个评论
Adam Glick
Adam Glick 2015-10-12
So I actually cannot execute my program like that because the function my GUI will execute at the end takes in lots of different user inputs from different functions.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by