matlabimagejimagej-macro

MIJ: execute imageJ (Fiji) macro until end in Matlab


I am using MIJ to execute an ImageJ macro within Matlab. The macro has to be executed multiple times in a "for" loop. The problem is that Matlab does not wait until the macro ends. Initially I solved the problem with a "while" loop, checking if the "Results" table generated from the macro was empty or not. However, it only solves the problem the first time, then from the second time the "Results" table is not empty anymore. I also thought about generating a variable at the end of the macro and use it to check if the macro finished, but I don't know how to read it in Matlab.

Do you have any suggestion about how I can solve the problem?

Thanks a lot in advance, Alessia

Here is an example of my code:

javaaddpath 'C:/Program Files/MATLAB/R2019a_x64/java/ij.jar'
javaaddpath 'C:/Program Files/MATLAB/R2019a_x64/java/mij.jar'
MIJ.start('C:/fiji-win64/Fiji.app/plugins');

IJ=ij.IJ(); 

macro_path=... 
'C:/Macro_waterinoil.ijm'; 
for pos=1:16
    im = mijread(strcat('E:/droplets.tif'));
    figure(1)
    imshow(im,[0 255])

   IJ.runMacroFile(java.lang.String(macro_path)); 

    res_Hough=0;  
    res_Hough=MIJ.getResultsTable();


    while res_Hough==0;   
        res_Hough=MIJ.getResultsTable();        
    end

    im_res=MIJ.getCurrentImage();

    MIJ.run('Clear Results');
    MIJ.run('Close All');
end

Solution

  • Edit: Ignore the below, I was on the right track but wasn’t paying as much attention as I should have been. I think this issue is the line right after you set res_Hough to 0 (i.e. res_Hough=MIJ.getResultsTable();). Try deleting it so that the next line is the while loop, then we can check the output and see if the below might also apply

    If I am understanding correctly, you are using the while loop to continuously ping until the table is full, then store those values in res_Hough, right? I'm wondering if this is a limitation inherent to ImageJ/FIJI. The reason I think this might be the case is that a very simple explanation for your issue is that the table retains the previous values, and so will always be full after the first loop unless cleared manually somehow. Do you think that could be the case? Perhaps add a print statement after the while loop and see if it prints the same values for the duration of the for loop.

    I suppose the next thing to do is for me to actually try to offer a solution regardless of the above. My idea is to try having the while loop check against the previous iteration's table values until they are different, then stash the updated values. Does that make sense? Something like:

    while (res_Hough[i]==0) or (res_Hough[i-1]==MIJ.getResultsTable());
        res_Hough[i]=MIJ.getResultsTable();
    

    Where i is incremented by the for loop