Why do I get an error when I enter f1(x)?

2 次查看(过去 30 天)
I'm trying to do a MATLAB work sheet using rand and every time I put f1(x) in the command window it says this:
>> f1(x)
Undefined function or variable 'f1'.
Did you mean:
>> f14(x)
Does anyone know why it is saying this?? The only command that I have entered before this is x=rand (not sure if that helps).
PLEASE HELP
  2 个评论
Stephen23
Stephen23 2016-3-26
What do you expect f1(x) to do? Have you defined functions or variable with the names f1 and x? If they do not exist (and they are NOT built in MATLAB functions), then of course MATLAB cannot find them and cannot call them.
Please show us the exact code that you are calling.
Gautam Mohan
Gautam Mohan 2016-3-28
Hi Helena,
If MATLAB does not have a function definition for the function 'f1', then it will try and suggest the closest matching name, which is 'f14'. It seems like you want to declare a behavior for the f1 function. Information about declaring functions can be found here: http://www.mathworks.com/help/matlab/ref/function.html
I suggest you create a file called f1.m and define the f1 function according to the guidelines in the above link. some sample syntax might look like this:
function out = f1(x)
% MATLAB code goes in here
end

请先登录,再进行评论。

回答(1 个)

Muhammad Usman Saleem
编辑:Muhammad Usman Saleem 2016-3-28
program which are writing is logically flaw. You have generated random no. Which is decimal not integral. While f1(x) means you are gaining f1 variable element located at the index define by x. As x is not integral that why this error is appear.
Let see it
x=rand
x =
0.3304
>> x=round(x)
x =
0
>> f1=[1:5]
f1 =
1 2 3 4 5
f1(x)
Subscript indices must either be real positive integers or
logicals.
Do not use random to assess element of variable f1

类别

Help CenterFile Exchange 中查找有关 Variables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by