My functions Won't Share Variables

조회 수: 1 (최근 30일)
Quinn
Quinn 2011년 7월 14일
Hello, I'm a student, attempting to write a data processing program with a GUI. I have an m-file with several functions in it, and I'm using this notation:
function [output variables]=GUI(input variables)
function [output variables]=fun1(input variables)
function [output variables]=fun2(input variables)
function [output variables]=fun3(input variables)
function [output variables]=fun4(input variables)
function [output variables]=fun5(input variables)
I use the GUI to gather all of the parameters for the algorithm functions, and I can't figure out how to get the functions to share variables. I am pretty sure that the scopes of the functions are not overlapping, but I don't know how to make this happen. What is the best way to make them share. Thanks!

답변 (5개)

Jan
Jan 2011년 7월 14일
You can use GUIDATA to obtain a struct, which is stored in the FIGURE of the GUI. Then all subfunctions need e.g. the figure handle as input and GUIDATA is used to get former values and write new values to the data.

Doug Eastman
Doug Eastman 2011년 7월 14일
You can share the workspace of the main GUI function with the subfunctions fun1-5 by making those subfunctions nested functions. Use the following syntax:
function GUI()
function fun1()
end
function fun2()
end
...
end
In this case fun1-5 can use any variables in the GUI function without needed to pass them in explicitly.
  댓글 수: 1
Quinn
Quinn 2011년 7월 14일
I already tried this, and I think maybe then my problem is how I'm calling the functions. There are uicontrols in the GUI which call each function:
function GUI()
uicontrol('callback',@(inputarg)fun1)
...
function []=fun1()
end

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2011년 7월 14일
Remember that shared variables must be defined (initialized) before the subfunctions are defined.

Quinn
Quinn 2011년 7월 14일
I simply deleted all of the input/output parameters for the subfunctions, and then they were able to freely share variables. It works now, but I'm curious as to why. Thanks for the responses.
function [all variables]=GUIfunction(all variables)
fun1 fun2 fun3 fun4 fun5 end
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 7월 14일
Your code in your original question shows the functions as having parameters, but your code in your comment in response to Doug shows your functions as not having any parameters; as the comment was later, I presumed that it was the more precise version.
The variables you were trying to share: were you naming them in the parameter list to fun1 and so on? If you were, then as is described in the documentation for shared variables, variables in a functions parameter list are never shared with variables from the outer scope. By deleting the names from the parameter list, you would have given the functions access to the shared versions of those variable names.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2011년 7월 15일

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by