Clear Filters
Clear Filters

In Matlab output of a called function cannot be given as input to another called function?

1 view (last 30 days)
Please refer to the above images.
The main file calls the input_val function and prints the output values. The matrix_vor function uses one of these values to find value of a array. But it shows the error of output not being defined.
I then removed the input function (i.e. first 4 lines of second image) and directly defined value of m just before calling the matrix_vor function. I am getting the output.
Can I imply that the output of first function (i.e. input_val) cannot be passed on as input for second function(i.e. matrix_vor)

Accepted Answer

Walter Roberson
Walter Roberson on 27 Aug 2013
In the second screen snapshot, refer to the two fprintf() lines before the call to the function. We can see there that m is the last value that is displayed by that fprintf() series.
In the third screen snapshot, refer to the "del1x" output line. That is the line being printed out by the two fprintf() referred to above. We see from that snapshot that the last value printed out is 0.7 so unless something went quite wrong in that step, m is 0.7
In the first snapshot, notice that the code goes directly from "function" to "for i=1:m", and d(i) is assigned to in that loop. Look further down in the code and notice that everything else there is commented out. So "d" will be assigned to in the routine if and only if the "for i" loop executes its body at least once.
Now, "for i=1:m" when m is 0.7, is "for i=1:0.7". As the first number is greater than the second, and the loop has not been told to count backwards, the body of the "for i=1:m" loop will not be executed at all. So "d" will not be assigned to by that code. Which triggers the error message you are seeing.
9 hours ago, I posted some advice for you here
At the command line, give the command
dbstop if error
and then run your program. When it stops with the error, display the value of n that the routine has received.
If you had done that, except displaying "m" for this code instead of "n", then you would have solved the problem already, and I wouldn't have had to type this all out when I could have been solving a different Question.
  1 Comment
Ashutosh
Ashutosh on 27 Aug 2013
I am sorry sir. Actually I did try dbstop command, but matlab got crashed. Hence I did not pursue that method forward and did a few experimentations myself. But now I am clear with the idea completely. Thanks a lot for your patience. Sorry for the inconvenience.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!