Function does not assigin the variable to the workspace

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

Can you provide more informations
I don't know that that is enough information for us to solve your problem. Are both functions in the same m-file? What are the names of the two functions (the former script that is now a function, and the function that the script called)?
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.
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

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

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?
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

The script is read.m. It reads some measurement data. I want to get some plots from these data and some statistical values. Because data are sometimes vague I make certain plots while the scripts executes. From the plots I see if the data need to be corrected or not. I have created a very simple gui function which is called panel. When I run the read.m. A Yes/No box pops up. I choose for example "Yes". Then the panel returns to the base
st=1
Two more times the box will appear and again the st will take a values.Either zero or one. I collect this values into an array named choice. So choice can be
choice=[1 0 1];
Then according to the script read.m continues and does further calculations. Now I want to turn the script read.m into a function. How is it going to find the variable "st"?
Indeed "st" is an input somewhere in the script and if I want this script to convert it into a function I have to provide the "st" input from the beginning of the function.
Since, the nature of the data requires my intervention to provide a Yes/No input at the middle of the execution what can I do provide this input while it executes.
Excellent description Jan. Giorgios, what's to prevent you from asking yes/no via questdlg() in your code when you want the user intervention? Also, don't use read() as a function name since it's the name of a built in function used for reading videos, among other things.
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.
@Giorgos: Unfortunately comments cannot trigger messages at the moment.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!