Printing function results to the Workspace

5 ビュー (過去 30 日間)
Stefan
Stefan 2011 年 4 月 6 日
So I have a simple function
function range(x,y)
%Finds the range between x and y
y-x
When I run the function, I would like the result to output to the workspace. If I run:
range(4,5)
ans =
1
So i want the Workspace to then show "Range" with Value = 1
My first thought was to type
range_a = range(4,5)
but it responds with:
??? Error using ==> range Too many output arguments.
Any ideas would be greatly appreciated!

採用された回答

Matt Fig
Matt Fig 2011 年 4 月 6 日
You get that error because you haven't given the function any output arguments. So let's give it one!
function R = range(x,y)
%Finds the range between x and y
R = y-x;
Now from the command line:
range(4,5)
Or,
RNG = range(4,5)
Also, you would probably find it worthwhile to read the function doc:
doc function
  1 件のコメント
Stefan
Stefan 2011 年 4 月 6 日
Thanks, that worked, and I was able to do more complicated things with it!

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

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2011 年 4 月 6 日
change your function definition
function out=range(x,y)
out=y-x;

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by