Why is my function not working?

function [y] = mysq(x) %UNTITLED Summary of this function goes here % Detailed explanation goes here y=x.^2; end

댓글 수: 1

Alex Mcaulley
Alex Mcaulley 2019년 10월 31일
What do you mean by "it's not working"? I think it does

댓글을 달려면 로그인하십시오.

답변 (1개)

the cyclist
the cyclist 2019년 10월 31일
편집: the cyclist 2019년 10월 31일

0 개 추천

Is everything on a single line, as you posted it here? If so, then everything after the first "%" is a comment, and MATLAB will ignore it. If it is formatted like this:
function [y] = mysq(x)
% UNTITLED Summary of this function goes here
% Detailed explanation goes here
y=x.^2;
end
then it should work.
If that is not the issue, please give us more detail about what "not working" means. Does it return a result that you do not expect, or does it give an error? If the latter, what is the complete error message?

댓글 수: 3

Ashley Leis
Ashley Leis 2019년 10월 31일
This is how it's formatted.
function [y] = mysq(x)
% UNTITLED Summary of this function goes here
% Detailed explanation goes here
y=x.^2;
end
When i "run" it this comes up.
function [y] = mysq(x)
Error: Function definition not supported in this context. Create functions in code file.
Ah. You should not just try to run that code in the command line. You need to create a separate file (call it mysq.m) with that code in it. Then you can call that function from the command line like
output = mysq([2 3 5 7])
John D'Errico
John D'Errico 2019년 10월 31일
And this is why it is hugely important, when someone says something does not work, to explain what did not work. Was there an error? If so, then what was the complete text of the error. This question was far easier to resolve when the error message was shown.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

질문:

2019년 10월 31일

댓글:

2019년 10월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by