Scilab pointer function - scilab

I am working on converting code from MATLAB to scilab included here.
The # symbol is used as a memory pointer in MATLAB pointing to the location of the function tst_callback.
Scilab does not like this however. Is there a scilab equivalent for the #?
function test
sysIDgui(#tst_callback)
end
function tst_callback()
disp("Hello Ron")
endfunction

What you are trying to do is to pass a function as argument to another function. In Scilab, you don't need any special syntax.
Try it yourself. Define these two functions:
function y = applyFunction(f,x)
y = f(x);
endfunction
function y = double(x)
y = x * 2;
endfunction
Then test it on the console:
--> applyFunction(double,7)
ans =
14.
Note: the main usage of # in MATLAB, is to create anonymous functions (see documentation), ones that are not defined in a separate file. As for Scilab, there is no way to create anonymous functions.

Related

Call a scilab function

I'm using scilab on cloud
function c = example(a,b)
c = a*b
endfunction
How can I call it?
I tried exec(example(3,2)) without success
This solution seems to work although obviously in this case I can't call the file before calling the function.
result = example(3, 2)

plotting a python function that uses an array

In sagemath, I would like to plot the following function foo (Coef is an array that is big enough) :
def foo(x):
x_approx = floor (x*4)
return Coef[x_approx]
I wanted to use the command plot(foo(x), (x,0,0.1)).
But I got the error unable to convert floor(4*x) to an integer.
Whereas when `foo is not using an array, it works:
def foo(x):
x_approx = floor (x*4)
return 4*x_approx
Use plot(foo, (x, 0, 0.1)) instead (that is, replace foo(x) with foo). If you use foo(x), then Sage tries to evaluate foo(x) first, in which case it treats x as a symbolic variable and can't turn it into a number to plot. If you use foo, then it knows to treat it as a plottable/callable function, and it does the right thing.
Edit: I think the issue is that for plotting, Sage requires a certain type of function, a symbolic function, and using a Python construct like Coef[...] doesn't fit into that framework.

How to define a function inside a function depending on variable values

I'm writing a function that I would find easier to write and read if it could define another function differently depending on input or runtime values of variables (and then use that function). The following illustrates the idea (even if defining a function inside a function is of no advantage in this simple example):
julia> function f(option::Bool)
if option
g() = println("option true")
g()
else
g() = println("option false")
g()
end
end;
WARNING: Method definition g() in module Main at REPL[1]:3 overwritten at REPL[1]:6.
julia> f(true)
option false
julia> f(false)
ERROR: UndefVarError: g not defined
in f(::Bool) at .\REPL[1]:7
Using the full function ... end syntax for g does not help either.
The question is: am I doing something wrong to get that warning and that unintended behavior, or Julia does not allow this for a reason? And if it can be done, how?
N.B. For my present need, I can just define two different functions, g1 and g2, and it seems to work; but what if there were many cases of g for just one task concept? I thought that a function, being a first-class object, could be manipulated freely: assigned to a variable, defined in a way or another depending on conditions, overwritten, etc.
P.S. I know I can compose a String and then parse-eval it, but that's an ugly solution.
You want to use anonymous functions. This is a known issue (this other issue also shows your problem).
function f(option::Bool)
if option
g = () -> println("option true")
else
g = () -> println("option false")
end
g
end
In v0.5 there's no performance difference between anonymous and generic functions, so there's no reason to not use anonymous functions. Note that there's also a sytnax for extended anonymous functions:
f = function (x)
x
end
and you can add dispatches via call overloading:
(T::typeof(f))(x,y) = x+y
so there's no reason to not use an anonymous function here.

Julia - can a global variable be used in a function before it is defined?

At this top of this page https://julia.readthedocs.io/en/latest/manual/performance-tips/#man-performance-tips under global variables, they use this example:
global x
y = f(x::Int + 1)
x can't be used in the function because it isn't defined. What exactly are they doing there?
This is just short hand notation. They are assuming that x gets defined elsewhere between its global declaration and the function call. Note that the function f() is not defined in that tiny snippet from the docs either.

Scilab multiple functons

I am working with scilab 5.4.1. Is there any way to define and invoke more than 1 function in .sci file. I have read in theory that there is a possibility. Does somebody know how?
This is possible, just define multiple functions in one file.
//First function
function x=myfct(a, b)
x=a+b
endfunction
//Second function
function y=myfct2(a, b)
y=a/b
endfunction
//Third function
function y=myfct3(a, b, c)
y = myfct(a,b)
y = myfct2(y,c)
disp(y)
endfunction
See the SciLab help pages for more information about functions.
After defining the above functions in a file and running the file once, the files are defined. You can now call them from console:
>> myfct3(3,4)

Resources