Calling a variable before it is created?

Hello all, I am calling a func (on line 10) it is using a variable that is defined in a set of lines(from line 100-110). In other words,i want to process a set of lines FIRST (which are present in middle of code and then continue the usual code). How can i do that? thanks alot

Answers (1)

Copy those set of lines from down below in your script or function to before line 10. If you want, you can make those set of lines into their own function and just call that function twice, once before line 10, and once inside the function that needs them.

5 Comments

Thanks alot. i tried it already. Problem is that those set of lines are using a variable that is defined in the middle of the code.
Let's say line 10 is this
output = fun(a,b,c);
and let's say that down at line 100 (middle of a 200 line script) you have this
a=10;
b=20;
c=30;
What values did you expect fun() to receive????? If you expect fun to receive the values assigned down at line 100, then up at line 10, insert them:
a=10;
b=20;
c=30;
output = fun(a,b,c);
Now fun() will have the values it needs. However you say you tried this and it didn't work, though I find that very difficult to believe. I guess you'll have to prove it by attaching your code.
Error: Undefined function or variable 'data'.
Error in: ts_cnvr (line 1)
for z=4:size(data,1)
Error in: func_retrieve (line 92)
ts_cnvr();
variable data is is being called in function 'ts_cnvr' in line1.
While it is being defined from line 20 till 85.(calling certain other functions on its way).
Thanks alot in advance for your time.
Why do you think it should know, in line 1 of ts_cnvr(), how many rows are in data when you did not pass it in to ts_cnvr()? You need to pass it in. In func_retrieve(), at line line 92, you call ts_cnvr() but did not pass it data. You need to define data somewhere in func_retrieve.m before line 92 so that you can then pass it to ts_cnvr like this
In func_retrieve.m file
% Some code .... then:
data = ...... % Somehow define or get data
% at line 92
output = ts_cnvr(data);

Sign in to comment.

Asked:

JB
on 15 Nov 2015

Commented:

on 15 Nov 2015

Community Treasure Hunt

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

Start Hunting!