How should I fix this function?

I made a very simple function where the output returns a 10 by 10 matrix with random integers between 2 inputted numbers. randi works fine but I am trying to test if the input is a real number. So if both inputs are integers, a matrix should be created. If not, the function should print an error message. I keep on getting an error message when I test whether either of the inputs are a real number and I can't figure out what my mistake is. Below is my code.
function rMatrix = main(startN,endN) if isreal(startN) == 1 && isreal(endN)== 1 r = randi([startN endN],10,10); rMatrix = r else rMatrix = print("Error") end end

1 件のコメント

Walter Roberson
Walter Roberson 2018 年 10 月 30 日
randi requires that the start value not exceed the end value, and that the start and end values are integral
Also the MATLAB function named print has to do with saving figures to files.
Also using " on strings requires r2017a or later.

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

回答 (1 件)

madhan ravi
madhan ravi 2018 年 10 月 29 日

0 投票

main2(1,10) %calling function here
function rMatrix = main2(startN,endN)
if isreal(startN) == 1 && isreal(endN)== 1
r = randi([startN endN],10,10);
rMatrix = r
else
error('Error')
end
end

2 件のコメント

Supriya Kannan
Supriya Kannan 2018 年 10 月 29 日
The code works but it runs as a script. When I call the function main2 again with different inputs, I get the error message "Undefined function or variable 'main2'."
madhan ravi
madhan ravi 2018 年 10 月 30 日
main2(1,10)% this goes to script file and the rest goes to function file .
Note: the function file should be named as main2 , please take note here.

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

カテゴリ

ヘルプ センター および File ExchangeApp Building についてさらに検索

質問済み:

2018 年 10 月 29 日

コメント済み:

2018 年 10 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by