help declare a function

1,114 views
Skip to first unread message

B15e63

unread,
Mar 28, 2009, 8:14:27 AM3/28/09
to freemat
I'm new to FreeMat. Trying to define a function. Have read the manual,
primer, online help. Tried in different ways :(

E.g. created a file "func.m" , containing

function c = addtest(a,b)
c = a + 2*b;

Calling the file from the console results in
-->
func.m
In base(base) on line
0
In docli(built in) on line
0
In Eval(func.m) on line
1
In func(func) on line
2
Error: Undefined function or variable
a
-->

I have it on a Mac (OX 10.5.6), but under WinXP the problem (and the
output) is the same.

What's wrong?

Thomas Benson

unread,
Mar 29, 2009, 10:54:24 PM3/29/09
to fre...@googlegroups.com

The search path may not contain func.m.  You can run 'which func' to see if the function is known or unknown.  If it is unknown, try adding the directory that it is in using addpath (e.g., addpath('c:\path\to\mfiles') using Windows-style paths).  You can permanently add paths to the search path using pathtool.

Regards,
Tom

Samit Basu

unread,
Mar 30, 2009, 10:35:21 PM3/30/09
to fre...@googlegroups.com
The error message you are getting is because the function "func"
requires two arguments "a" and "b", and you are calling the function
without defining the arguments. If instead you type:

--> func(3,5)

ans =
13

Samit

B15e63

unread,
Apr 1, 2009, 4:54:09 AM4/1/09
to freemat
Thank you Samit, it works

Actually the problem was, I have tried to first call the file on the
prompt "func.m" as I didn't realize that apparently the functions
within the path are downloaded automatically. After reading the manual
once more, I couldn't find any explicit indication on such behavior.
Which behavior I personally find not obvious. Samit, maybe amend the
manual on that point?

Ben

B15e63

unread,
Mar 30, 2009, 1:53:03 PM3/30/09
to freemat
Tom, thank you for your advice. However:

1. FreeMat actually finds and calls the file "func.m" (which produce
the errors)
2. Consequently, 'which func' results in "Function func, M-File
function in file '/Applications/FreeMat/func.m'"
3. Tried to add the whole path to the '/Applications/FreeMat/' and all
subfolders using the pathtool. Didn't help.

Still at loss,
Ben

Thomas Benson

unread,
Apr 1, 2009, 10:51:35 PM4/1/09
to fre...@googlegroups.com

Oops, sorry about that.  I didn't look very closely at the error message.

Incidentally, does anyone else forward their Gmail to another account?  It seems to be very slow.  I didn't get this email until about 30 minutes ago and it was sent almost three days ago.  I actually got Samit's response first (but didn't get it until a day after it was sent).  I also just got a batch of the other emails sent on March 30.  Maybe that's Google's way of trying to get me to actually login to my Gmail account...

T.Cy...@gmail.com

unread,
Apr 2, 2009, 7:54:27 AM4/2/09
to Thomas Benson, fre...@googlegroups.com
I'm having the same issues, but I'm not forwarding, it's just not getting to my inbox until several days later. Perhaps it's an issue with googlegroups or something?

TJ

Garystar

unread,
Apr 4, 2009, 10:29:19 AM4/4/09
to freemat
I'll be sure to include this as a part of the next version of the
"Primer". Sorry you had this frustration, but it won't have been
wasted!

Gary

Timothy Cyders

unread,
Apr 4, 2009, 12:45:09 PM4/4/09
to fre...@googlegroups.com
By the way Gary, I'm working on a whole chapter for the primer on some basic numerical analysis techniques and how to implement them in simple scripts in FreeMat. It will probably take another couple weeks, but I'll get it to you as soon as I can.

TJ

Oyster

unread,
Apr 19, 2009, 1:36:39 AM4/19/09
to freemat
any idea to declare a function in code?
I mean
[code.m]
function c = addtest(a,b)
c = a + 2*b;
end function

print c(1,2)
[/code.m]

I know that is not a matlab style, but I do think that matlab's
implement is an most ugly one. For most of the modern langauge, we can
delcare a function and call it in the same code.
This ugly things lives in matlab's class

for declare a function, there are 2 styles in www.freebasic.net (and
some of otehr basic)
[style 1]
function addtest(a,b)
addtest = a + 2*b #we assign the result to the
function name, then it is returned automatically
end function
[/style 1]


[style 2]
function addtest(a,b)
res = a + 2*b
return res #we return it
end function
[/style 2]

Timothy Cyders

unread,
Apr 19, 2009, 11:41:41 AM4/19/09
to fre...@googlegroups.com
I'm not exactly sure what youa re asking, but to declare a function in FreeMat is very much like your first example -

function return_value = fun_name(arg1,arg2)
     return_value = arg1 + arg2;
end

If you don't wish to specify a return value (which is returned as ans to the FreeMat terminal upon execution of the function), you can simply say

function fun_name(arg1,arg2)
      arg1+arg2 % note output is not suppressed
end

If you have a function and you want to define a second funciton in the same code, you can do this:

function return_value = fun_name(arg1,arg2)
     return_value=subfun(arg1,arg2);
end

function subreturn = subfun(arg1,arg2)
      subreturn = arg1+arg2;
end

and the effect will be the same. Hope this helps!

TJ
Reply all
Reply to author
Forward
0 new messages