Write function in loop

I would like the output files of my program to have the same label as the input files. So I used that bellow code, but it doesn’t seems that it’s working.

name = name_data[12:-6]

write(ML, "myResults/Separable_Linear_"*name*".fits")

Here’s it the name of one of my name_data: “Model_Synthetic_Star_0.2_incl=90_pa=180_e=0_wave=90”.

Thank you in advance

That way of indexing as in Python does not work in Julia. You should use end-n to refer to the last but n-th element.

2 Likes

Thank you I got.
Here’s the solution that I found out.
name_data=“model/THENAMEOFYOURFILE.fits”, “model/” is composed of 6 char, so I should take [7:end-6]


name = name_data[7:end-6]

write(ML, "myResults/Separable_"*name*".fits")

Like that I got the same label as the input files as the output files.

Ok, you can also get the parts of the path and file names independently from their actual length with functions as splitpath, splitdir or splitext.

1 Like

And this package seems also very well suited to your use case

2 Likes

Not really relevant to your question but rather than creating a variable to contain the string section and then another new string for the filename, you can use interpolation directly on the original string

"myResults/Separable_Linear_$(name_data[7:end-6]).fits"