Function does not assigin the variable to the workspace

5 views (last 30 days)
I have created a script that calls a function and the last one assigns in the workspace a variable and this works perfectly.
I am trying to turn the first script into function. In this case I get an error:
Undefined function or variable 'st'.
'st' is the variable that is assigned in the script. But when I turn it into a function then the variable is no more assigned.
Why?
  4 Comments
Giorgos Papakonstantinou
Giorgos Papakonstantinou on 4 Jun 2013
I will try to be as specific as possible. I have a large script and it is running without any problem. I would like to turn it into a function. The script does some calculations and at a certain point it calls a function with the name "panel".
for n=1:length(STR)
f=panel(STR(n));
waitfor(f); % f is "YES"/"NO" box so waitfor(f) to close
choice(n)=st;
end
STR is cell array of strings that is used as an input for the function panel. The panel is gui that I have made myself. I call it three times in the script and it pops up a "yes"/"no" box. According to the choice it assigns to the base the variable "st". I use inside the gui the command
assignin('base','st',st)
Then the variable
choice(n)=st;
and according to the choice the script continues and runs without a problem.
I try now to convert this script into a function. Apparently inside this function there is the function "panel" that assigns to the workspace the variable st but probably the main function cannot "see" the variable st so I get the error:
Undefined function or variable 'st'
I hope that I made it a bit more clear. Thank you.
Giorgos Papakonstantinou
Giorgos Papakonstantinou on 4 Jun 2013
Image analyst I have two files. The one is the main script and the other is the function named "panel". I want to turn the main script into a function. So they are both two separate files in the same directory.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 4 Jun 2013
Edited: Matt J on 4 Jun 2013
assignin('base','st',st)
will send 'st' to the workspace of the command line. It will not send it to the workspace of any arbitrary function that you want. You should use the GUIDATA command when st is generated to store it among the rest of the GUI's data. Then use GUIDATA again to retrieve it in the function that does
choice(n)=st;
  2 Comments
Giorgos Papakonstantinou
Giorgos Papakonstantinou on 5 Jun 2013
After trying a lot. I figured out how to do what you suggested. I have another related question. I have used the examples of gui of Matt Fig GUI Examples. In none of them guidata command is used. Specifically in his guidelines he explains that. My question then is how to pass the data from the callback to the gui. To be more precise my callback is this:
function hide(varargin)
g = varargin{3};
str=[get(g.rd(1),'val'),get(g.rd(2),'val')];
if str(1)==1
g.st=true; % yes
else
g.st=false; % no
end
guidata(g.fh,g.st);
uiresume
and I want to pass the value of g.st from this callback to the gui. I cannot do it without guidata. Is there any other way?
Iain
Iain on 5 Jun 2013
You need the handle of an item on the gui. If you have that you can put any information you like into the "Userdata" property:
set(handle,'Userdata',variable)

Sign in to comment.

More Answers (1)

Jan
Jan on 4 Jun 2013
Edited: Jan on 4 Jun 2013
Let me guess the details (but please post more details in the future even when this guess is successful):
The script file test.m:
st = 23
Now you type in the command window:
test % Script is called
st % 23 is replied
Then you convert the script to a function:
function test
st = 23
And call it from the command window:
clear st % cleanup
test % function is called
st % ERROR: Undefined
Or perhaps you've considered the output already:
function st = test
st = 23
And call it from the command line:
test % function is called
st % ERROR: Undefined
Still an error, but why? st is created inside the function, but it is the purpose of functions to keep the definitions inside except for the exported outputs. But these outputs must be caught be the caller:
st = test
st % Replies 23
The name need not be equal:
abc = test
abc % Replies 23
The behavior of functions is explained in the Getting Started chapters of the documentation. It is worth to read them, when you want to use such a powerful language as Matlab. It is not the intention of the forum (but obviously mine in this evening) to repeat the basics, which are written down exhaustively in the docs already.
  4 Comments
Giorgos Papakonstantinou
Giorgos Papakonstantinou on 6 Jun 2013
Thank you for the advice about the read. The actual name of the function is dektak_read. But I just wanted to make things more simple... Anyway I have to thank all of you for helping me in this. It seems that the same few people are replying and giving solutions. Thank you once more.
It's the second time you asked this question Image analyst -:)!!!! I have replied here. I don't have anything against questdlg. It is sufficient for what I need to do at the moment. On the other hand by this process I learned the basics of making a gui. And who knows???? It might be useful for the future. It's been only a few months that I am involved with MATLAB due to my thesis and I need to learn new staff. I find this process fun, useful and didactic.
By the way I is there anyway to get notified when somebody makes a comment? Because I am not getting notified. I have to check regularly to see if somebody made one.
Jan
Jan on 6 Jun 2013
@Giorgos: Unfortunately comments cannot trigger messages at the moment.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!