Login  Register

Re: replace substring of filename with string

Posted by OwenGwydion on Feb 26, 2018; 1:57pm
URL: http://imagej.273.s1.nabble.com/replace-substring-of-filename-with-string-tp5020154p5020158.html

Hi Mike,

Thanks for your help in pointing me in the right direction.

I ended up renaming the files to show a consistent filename to the plugin.
It now runs smoothly.

For interest I've copied it below:



        function checkSelection(){
                Dialog.create("Prepare to start stitching files");
                Dialog.addMessage("Choose which Well+Channel folder you want to stitch, or
select 'Exit macro' when finished");
                Dialog.addChoice("Folder to stitch", wellChannelList); //Choose
which channel you want to stitch
                Dialog.addCheckbox("Exit macro", false); //Opt to exit macro
                Dialog.show();
                Exit=Dialog.getCheckbox();
                Choice=Dialog.getChoice();
                tempDir=newDir+Choice;
                print("the TempDir is: "+ tempDir);
                ChoiceIndex=indexOf(tempDir, "Well");
                ChoiceNameEnd=lengthOf(tempDir);
                ChoiceName=substring(tempDir, ChoiceIndex, ChoiceNameEnd-1);
//Extracts the name of the chosen file to be used for saving stitched image
                print("the choiceName is: "+ ChoiceName);
                File.makeDirectory(tempDir); //Creates new directory based on
user choice called tempDir
                File.isDirectory(tempDir); //Returns 1 if tempDir is a true
directory
                tempList=getFileList(tempDir);
                Array.print(tempList);

               
                        for(i=0; i<tempList.length; i++){

                                // identifies the full site name and replaces with variable {ii}
                                if(endsWith(tempList[i],".tif") || endsWith(tempList[i], ".TIF") &&
(matches(tempList[i], ".*Stitched.*") != 1)){
                                        currentName=tempDir+tempList[i];
                                        indexSiteX=indexOf(tempList[i], "_s");
                                        indexEndSiteX=indexOf(tempList[i], "_w");
                                        firstPart=substring(tempList[i], 0, indexEndSiteX+3);
                                        shortenedName=firstPart + ".tif";
                                        print("the ShortenedName is: " + shortenedName);
                                        newTempName=tempDir+shortenedName;
                                                tempSiteName=substring(tempList[i], indexSite, indexEndSite);
                                                genericFileName=replace(shortenedName, tempSiteName, "_s{ii}");
                                                File.rename(currentName, newTempName);
                                                print("the generic File Name is: " + genericFileName);
                                }}

                                // run grid/collection stitching
                                if(Exit==false){
                                                print("Prepare to stitch: " +Choice);
                                                run("Grid/Collection stitching", "type=[Grid: row-by-row] order=[Right
& Down                ] grid_size_x=Xtile grid_size_y=Ytile tile_overlap=10
first_file_index_i=1 directory=[&tempDir] file_names=[&genericFileName]
output_textfile_name=TileConfiguration.txt fusion_method=[Linear Blending]
regression_threshold=0.30 max/avg_displacement_threshold=2.50
absolute_displacement_threshold=3.50 compute_overlap
computation_parameters=[Save memory (but be slower)] image_output=[Fuse and
display]");
                                                saveAs("tiff", newDir + ChoiceName + "_Stitched_image" + ".tif");
                                                checkSelection();   //loop to provide option for stitching
another channel, or exit macro
                                                }
                                                else {
                                                exit("You have exited the macro")
                                                }
                                       
}

Cheers,

Owen

///////////////////////////////////////////




--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html