creating a function without creating a class

Probably a stupid question, but i have a test file, which is just a set of commands and instantiation of some classes, but doesn't have any classes. I want to create a function in this file. Is it possible to create a function without having a class.

댓글 수: 1

@Jagdish Ashok: if you are trying to define a function in the middle of a script then this will not works. Scripts and functions cannot be mixed together on one file.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2016년 7월 5일

0 개 추천

That just sounds like a regular function file to me. For example,
function test_it_out
s = serial('COM5'); %creates an object of class serial
t = vision.VideoReader('rhinos.avi'); %creates an object of class vision.VideoReader

댓글 수: 4

I just want a basic function
function = testfunction(x,t)
y = AnotherFunction(x,t)
end
but i get this error
FUNCTION Keyword is invalid here, this might cause later messages about END
I guess this is because i have this function without any class.
function y = testfunction(x,t)
y = AnotherFunction(x,t)
end
A function statement should either have one or more outputs like I show here, or else it should have no "=", like
function testfunction(x,t)
y = AnotherFunction(x,t);
disp(y)
end
Classes is not a factor in this.
JA
JA 2016년 7월 5일
Check the pic below, please
per isakson
per isakson 2016년 7월 7일
편집: per isakson 2016년 7월 7일
It's the documentation that's a bit "stupid". It doesn't clearly states that the line that declares a (primary/main) function must not be preceded by any executable lines - only by comments.
Your function, testfunc, will work nicely if you put it in a separate file.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

태그

질문:

JA
2016년 7월 5일

편집:

2016년 7월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by