Why is my function not working?
Ältere Kommentare anzeigen
function [y] = mysq(x) %UNTITLED Summary of this function goes here % Detailed explanation goes here y=x.^2; end
1 Kommentar
Alex Mcaulley
am 31 Okt. 2019
What do you mean by "it's not working"? I think it does
Antworten (1)
the cyclist
am 31 Okt. 2019
Bearbeitet: the cyclist
am 31 Okt. 2019
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 Kommentare
Ashley Leis
am 31 Okt. 2019
the cyclist
am 31 Okt. 2019
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
am 31 Okt. 2019
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.
Kategorien
Mehr zu Entering Commands finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!