error: Too many output arguments

1 回表示 (過去 30 日間)
Marco Barbarossa
Marco Barbarossa 2018 年 9 月 2 日
コメント済み: Image Analyst 2018 年 9 月 3 日
my goal is to obtain a matrix A (2x2) using a function called somma:
function [] =somma (x,y)
z=x+y
my script code is :
clear all
clc
x=1:2;
y=3:4;
A=zeros(length(x),length(y));
[x1,x2]=meshgrid(x,y);
for i=1:length(x);
for j=1:length(y);
A(i,j)=somma(x1(i,j),x2(i,j));
end
end
A
the error reported is:
Error using somma
Too many output arguments.
Error in untitled5 (line 9)
A(i,j)=somma(x1,x2);

採用された回答

Image Analyst
Image Analyst 2018 年 9 月 2 日
You forgot to have somma return any value! Have it return z and it will work:
clc
x=1:2;
y=3:4;
A=zeros(length(x),length(y));
[x1,x2]=meshgrid(x,y)
for i = 1 : length(x)
for j = 1 : length(y)
A(i,j) = somma(x1(i,j), x2(i,j));
end
end
A
function [z] =somma (x,y)
z = x + y
end
  2 件のコメント
Marco Barbarossa
Marco Barbarossa 2018 年 9 月 2 日
now my script code is:
clear all
clc
x=1:2;
y=3:4;
A=zeros(length(x),length(y));
[x1,x2]=meshgrid(x,y);
for i=1:length(x)
for j=1:length(y)
A(i,j)=somma(x1(i,j),x2(i,j))
end
end
A
function [z] =somma (x,y)
z = x + y
end
my function code is:
function [z]=somma(x,y)
z=x+y;
end
errore reported :
Error: File: untitled5.m Line: 15 Column: 1
Function definitions are not permitted in this
context.
Walter Roberson
Walter Roberson 2018 年 9 月 2 日
Store the code for the function in a separate file. Having a function in a script file requires R2016b or later.

サインインしてコメントする。

その他の回答 (2 件)

ahmed nebli
ahmed nebli 2018 年 9 月 2 日
when writing the for loop it dosen't end with a ; and next the second for loop need to be under the first for loop and decaled by one tab, see syntax in this example https://www.mathworks.com/help/matlab/ref/for.html
  1 件のコメント
Marco Barbarossa
Marco Barbarossa 2018 年 9 月 2 日
same error message with this code:
clear all
clc
x=1:2;
y=3:4;
A=zeros(length(x),length(y));
[x1,x2]=meshgrid(x,y);
for i=1:length(x)
for j=1:length(y)
A(i,j)=somma(x1(i,j),x2(i,j));
end
end
A

サインインしてコメントする。


Marco Barbarossa
Marco Barbarossa 2018 年 9 月 3 日
thanks to all! Walter Roberson is right !
  1 件のコメント
Image Analyst
Image Analyst 2018 年 9 月 3 日
Or, better if you can, upgrade your release to the latest version.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by