Using uimenu with multiple output nested function

8 vues (au cours des 30 derniers jours)
Jeremy
Jeremy le 22 Déc 2012
I am attempting to use a the uimenu to access a nested fcn that has multiple variables I would like to be accessible from within the parent fcn. I tried using assignin to put the variable in the caller workspace, but I get the error "Error using assignin Attempt to add "coord" to a static workspace." Here is an example of what I am thinking:
uimenu(file,'Label','Open','Callback',@(varargin) openfile);
uimenu(tables,'Label','Nodes','Callback',@(varargin) fcnA(a));
function [a,b,c] = openfile
a=xlsread(obj,'a');
b=xlsread(obj,'b');
c=xlsread(obj,'c');
end
function fcnA(a)
%some function of 'a'
end
Perhaps if there is some other way to go about this, I am open to ideas. Thanks

Réponse acceptée

Jeremy
Jeremy le 22 Déc 2012
I ended up kind of tricking the code into a work around to solve this problem. Here's what it looks like:
function parent
x=1;
uimenu(file,'Label','Open','Callback',@(varargin) openfile);
uimenu(tables,'Label','Nodes','Callback',@(varargin) fcnA(a));
if x==12938
a; b; c;
end
function = newfile
a=0; b=0; c=0;
end
function = openfile
obj=uigetfile;
a=xlsread(obj,'a');
b=xlsread(obj,'b');
c=xlsread(obj,'c');
end
function fcnA(a)
%some function of 'a'
end
end
I believe this works because the parent function sees that a,b,c are being used in the if statement (which is never reached) and thus the nested functions assign the variables to the parent functions workspace. Defining the variables outside of the nested functions seems to overwrite them when trying to define them within the nested functions, but calling those variables without them being defined causes errors. Thus the code thinks that it will call those variables (through the if statement) but doesn't actually have to call them.
Hope this helps anyone else who runs into the problem.

Plus de réponses (2)

Walter Roberson
Walter Roberson le 22 Déc 2012
Assign a value (any value!) to the variable before you define the nested functions. Once you have done that, the nested functions will be able to read and write the variable, without needing any "assignin".
For example,
function test_nested
a = []; b = []; c = [];
uimenu(file,'Label','Open','Callback',@(varargin) openfile);
uimenu(tables,'Label','Nodes','Callback',@(varargin) fcnA(a));
function openfile
a=xlsread(obj,'a');
b=xlsread(obj,'b');
c=xlsread(obj,'c');
end
function fcnA(a)
%some function of 'a'
end
end
  1 commentaire
Jeremy
Jeremy le 22 Déc 2012
This didn't work. Could be because a, b, and c are also defined in another nested function?

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 22 Déc 2012
openfile() does not have any input arguments, so obj inside of it is undefined. Also, I'm not sure 'a' is a valid range in Excel - you probably need to have both a starting cell designation and an ending cell designation, like 'A1:D42'.
Beyond that, I'm not really sure what you're trying to do. Are you trying to add a pulldown menu to an existing figure?
  5 commentaires
Shaun VanWeelden
Shaun VanWeelden le 22 Déc 2012
" I assume this will end up only loading files from the current directory though " - You can change the search path pretty easily to whatever root directory you want, user-defined or pre-defined. Hope that helps as you move forward. Let me know if you have questions

Connectez-vous pour commenter.

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by