How to solve error 'Error: File: function9.m Line: 10 Column: 5 Function definition is misplaced or improperly nested.'

조회 수: 4 (최근 30일)
function function9
clc
clear all
close all
Problem_number=input('please enter question number')
if Problem_number==1
kmh=input('input kilometers');
ftps=problem_1(kmh);
function ftps=problem_1(kmh)
%function problem_1 converts kilometers per hour to meters per second
ftps=kmh*3281/3600
end
elseif Problem_number==2
days=input('enter the number of days');
hours=input('enter the number of hours');
minutes=input('enter the number of minutes');
minout=problem_2(days,hours,minutes);
function minout=problem_2(days,hours,minutes)
minout=minutes+(hours*60)+(days*1440)
end
elseif Problem_number==3
x=input('enter a value for x');
y=input('enter a value for y');
[theta,radius]=problem_3(x,y);
function [theta,radius]=problem_3(x,y)
theta=atan2(y,x)
radius=sqrt(x^2+y^2)
end
elseif Problem_number==4
X=[-3 3];
Y=problem_4(X);
function Y=problem_4(X)
Y=0.9*X.^4-12*X.^2-5*X
end
X2=linspace(-4,4,9);
Y2=problem_4(X2);
plot(X2,Y2)
xlabel('x from -4 to 4')
ylabel('problem_4(x)')
title('Problem 4 Graph')
end
end
%My code will not link the if-else statements after the first one and I keep getting improperly nested errors

답변 (1개)

Steven Lord
Steven Lord 2020년 3월 31일
You cannot define a nested function inside an if statement. This is the second item listed in the "Requirements for Nested Functions" section on this documentation page.
Move the definition of the function to the end of your main function if you need it to be nested inside that function or move it after the end of the main function if it doesn't need to be nested.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by