Saving image file, location by extracting script path


I’m using Scilab 2024.0.0 on a Windows 10 machine.

If I execute the following code:

// Extract the filename and path of this script:
[units,typs,nams]=file(); // nams(1) = script file name incl. path
fpathname=strsplit(nams(1), [filesep()]);
scriptpath = get_absolute_file_path(fpathname($));

It cannot get the proper information. This means when I try to save a plot:

saveplot = %t;
if saveplot then
xs2png(gcf(),xs2png(gcf(),scriptpath + "" + “output.png”));

Then I get the following error in the Scilab console:

xs2png: Unable to create export file, permission denied.

In the past this used to work, but I admit now it doesn’t work in older Scilab releases, like 2023.0.0 or even Scilab 6.1.0, so maybe it’s a Windows issue.
The script is located in a user directory with write permissions. Maybe Scilab doesn’t have the user permissions?
Maybe today there is another preferred way?

With kind regards,


With a bit of help from the side, I was able to fix the problem. At some point the file() output to nams contains more variables, so it should be nams(2) instead of nams(1) … and, oh, some copy error made the xs2png(gcf() appear twice - sorry. Here is the (minimal) code that works:

saveplot = %t;
// Extract the filename and path of this script:
[units,typs,nams]=file(); // nams(2) = script file name incl. path
fpathname=strsplit(nams(2), [filesep()]);
scriptpath = get_absolute_file_path(fpathname($));
// Plot some data:
plt = scf();
plot([2 3],[4 5]);
if saveplot then 
    fnams = scriptpath + "\" + "myplot.png";