[scilab-Users] Scilab equivalent of mfilename?

Serge Steer Serge.Steer at inria.fr
Tue Nov 15 19:17:55 CET 2011


Le 15/11/2011 06:33, Iai Masafumi ax a écrit :
> I am wanting something like __FILE__ macro of C language. It should be 
> useful in debugging and in managing a lot of data files and figures 
> generated by different scripts.
>
> The code you provided did not work as I wanted when multiple script 
> files are nested. To solve this, slight modification is made:
>
> [u,t,n]=file();
> i = grep(n',"/(?:.*\.sci|.*\.sce)$/","r");
> p = n(i(length(i)));   // <--- modified.
>
> Still, this method does not give me the file name of *.sci itself. 
> Suppose you have two files, fileA.sci and fileB.sce. A function is 
> defined in fileA.sci and is called from fileB.sce. If you put the 
> above code in fileA.sci and execute the script, fileB.sce, then you 
> get "fileB.sce" in the varialbe, p.
>
> Is there any way to get the file name of sci file as well?
>
>
> Iai
>
>
> (2011/11/15 11:23), Samuel Gougeon wrote:
>> Le 15/11/2011 01:21, Iai Masafumi ax a écrit :
>>> Hi
>>>
>>> Does Scilab have a command like mfilename in Matlab? I want to get the
>>> name of a currently running script file.
>>>
>> If you really don't know the name of the current script, you may use the
>> following:
>>
>> [u,t,n]=file();
>> i = grep(n',"/(?:.*\.sci|.*\.sce)$/","r");
>> p = n(i(1))
>> // and if you need only the script name without its path nor the file
>> extension:
>> basename(p)
>>
>> Samuel
>>
>
In scilab the  functions are not linked to a file name because it is 
possible to define a function in a script. Take also care that functions 
are Scilab variables (like matrices)

function n=functionname()
   [linenum,mac]=where()
   n=mac(2)
endfunction


example

function y=foo(x)
disp(functionname())
    y=x+1
endfunction

foo(3) //the calling function is named "foo"
A=foo
A(3) //the calling function is named "A"


Serge Steer
INRIA




More information about the users mailing list