nested function, Vectors, Switch statement Inquiry
1 次查看(过去 30 天)
显示 更早的评论
If I wanted to nest a function that takes three inputs within another function that takes three inputs, have the last two inputs of the main function to be equal to the last two digits of my nested function and have my first input of my main function, which is a vector, be named outside of the nested function but still be equivalent to the first input of my nested function. I would also like to use a switch statement to choose the values of my vector to plot. Would I be going about it the right way as shown below: (At the end I would like to have a single plot for each vector item):
% Main function
function NumOne = PrimaryFunction(FirInput, SecInput, ThirdInput)
% This is my vector which is also my first input
FirInput = [];
%These are my last two inputs which will be equivalent to the last two of the nested function
SecInput = secInput;
ThirdInput = thirdInput;
% This is my nested function
function numOne = SecondaryFunction(firstInput, secondInput, thirdInput)
% Here I'm making my secondary first input equivalent to my primary first input
firstInput = FirInput;
% set the duration for how long I would like my equation to be calculated for
thirdInput = 0:0.1:2;
% Here is the main purpose of my nested function, to make a linear equation
numOne = firstInput + (secondInput)
% switch statement to fill my vector
switch firstInput
case 1
firstInput = 50;
case 2
firstInput = 40;
case 3
fristInput = 30;
end
plot(thirdInput,numOne,'b');
xlabel('thirdInput')
ylabel('x(thirdInput)')
end
end
When I run I am stuck with this error:

Is there a way to make my nested function a global variable so I don't get this error? Feel free to advise me on other mistakes I may have made as well thank you
2 个评论
Stephen23
2019-1-27
编辑:Stephen23
2019-1-27
Related earlier question:
Note that you do not actually call the nested function anywhere, so defining it serves no purpose whatsoever. Nor does the nested function change any variables in the main workspace, so it is not clear what possible purpose it has being a nested function (as opposed to a simple local function), even if you did call it. And you ignore most of the nested function's input arguments (by redefining them), so why bother defining it with three input arguments that you discard without using?
That error is possibly caused by how you are calling the main function: do you call it with three input arguments? Please show us the exact way that you call the main function.
"Is there a way to make my nested function a global variable so I don't get this error?"
Why on earth would you want to ruin a perfectly good nested function with awful global variables?
It would probably help if you explained what you want to do with this: how should this be used?
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!