Using uimenu with multiple output nested function

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

 Accepted Answer

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.

More Answers (2)

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 Comment

This didn't work. Could be because a, b, and c are also defined in another nested function?

Sign in to comment.

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 Comments

That was just because I was trying to save a bit of space. The actual code reads more like this:
function [coord,edi,bc,el,sd] = openfile
obj=uigetfile('Bridge_data.xls','.xls');
coord=xlsread(obj,'Nodes');
where 'a' is designating a sheet name, not a range and obj is defined by opening the particular file. I am trying to open a file with one pulldown menu and then be able to access the data from that file in another pulldown menu.
Why not just load up a listbox with all the .xls files. Would you consider using GUIDE, or do you like to roll your own?
I prefer to do the coding. Gives me a better feel for what's going on and helps keep me from running into issues trying to figure out where in the code the GUIDE is doing stuff.
I could definitely do a listbox, I assume this will end up only loading files from the current directory though, so it may lead to issues elsewhere. My eventual plan is to make an executable file through the MATLAB C-compiler that runs like any other program and is easy to use for non-programmers.
" 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

Sign in to comment.

Categories

Asked:

on 22 Dec 2012

Community Treasure Hunt

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

Start Hunting!