Hello, I have been trying to do a curve fit in a macro but the curve fitter commands do not show up in the macro recorder. What would be the commands needed to open a txt file and do a Gaussian curve fit? I need the output windows you usually get from the curve fitter to proceed with the macro.

Any help appreciated.

# Using curve fitter in macro

**cgw_she**#1

**dnmason**#3

If you want another example of `Fit.doFit("Gaussian", X, Y);`

in action, check out this code I use to fit curves to spots.

https://bitbucket.org/davemason/fwhm_spotanalysis

The fun stuff starts around line 103.

**cgw_she**#4

Okay, I now know that I would need to use

`Fit.doFit("Gaussian", X, Y);`

but I canât get my data in (âOPENâ in curve fitter)

At the moment I got this:

```
info = getTitle;
run("Split Channels");
dir1 = getDirectory("Choose Directory for output");
for (i=1; i<5; i++){
selectWindow ("C"+ i +"-" + info);
//find the central slice of the stack and number of pixels in image
premiddle = nSlices;
middle = (premiddle/2);
mid= round (middle);
pixel = getWidth();
//show orthogonal projection and sum up
run("TransformJ Turn", "z-angle=0 y-angle=90 x-angle=0");
run("Z Project...", "projection=[Sum Slices]");
//analyse
name = getTitle;
//setTool("line");
//set linewidth to 3;
linewidth = 3;
setLineWidth(linewidth);
makeLine(mid, 0, mid, pixel);
run("Plot Profile");
Plot.getValues(x, y);
for (d=0; d<x.length; d++)
print(x[d], y[d]);
//read out data
selectWindow("Log");
saveAs("txt", dir1 + name + "plot data");
**run("Curve Fitting...");**
** //get data into curve fitter and fit**
** waitForUser("data required", "upload output file in curve fitter");**
** waitForUser ("run gaussain fit");**
** selectWindow("y = a + (b-a)*exp(-(x-c)*(x-c)/(2*d*d))");**
//safe data from fit
selectWindow ("Log");
saveAs ("text", dir1 + name);
print("\\Clear");
close ("Curve Fitter");
}
run("Close All");
```

I am saving the data as txt without tab - if you open that in the curve fitter it works fine.

To âautomate betterâ I tried to change the bit thatâs below run (âCurve FittingâŚâ) but I canât read my txt file in. I would like to use this (or similar):

```
pathfile=File.openDialog("Select the file to Open:");
filestring=File.openAsString(pathfile);
rows=split(filestring, "\n");
x=newArray(rows.length);
y=newArray(rows.length);
for(i=0; i<rows.length; i++){
columns=split(rows[i],"\t");
x[i]=parseInt(columns[1]);
y[i]=parseInt(columns[2]);
print(x[i]);
print (y[i]);
}
Fit.doFit("Gaussian", x, y);
Fit.logResults
```

I assume my problem lies here: `rows=split(filestring, "\n");`

I tried renaming the txt file to get a csv file but that didnât work.

Any help appreciated.

**dnmason**#5

I think youâre problem is that youâre thinking like a human not a computer. Unless you need to save out the profile to a file for something else, you can just pass the data straight to `Fit`

. You also donât need to plot the profile to get the values out - `getProfile()`

does everything for you.

This is what I would go for (although I donât have TransformJ installed so I canât run the whole thing):

```
info = getTitle;
run("Split Channels");
for (i=1; i<5; i++){
selectWindow ("C"+ i +"-" + info);
//find the central slice of the stack and number of pixels in image
premiddle = nSlices;
middle = (premiddle/2);
mid= round (middle);
pixel = getWidth();
//show orthogonal projection and sum up
run("TransformJ Turn", "z-angle=0 y-angle=90 x-angle=0");
run("Z Project...", "projection=[Sum Slices]");
//analyse
name = getTitle;
linewidth = 3;
setLineWidth(linewidth);
makeLine(mid, 0, mid, pixel);
//-- Added in to pull the values (equivalent of "Plot Profile" then "List")
profileY=getProfile();
//-- Make a set of X points. This is in pixels to if you need Standard Deviation in units, create a calibrated array of values by multiplying the array values by the pixel size
profileX=Array.getSequence(profileY.length);
//-- Set to output results to log
Fit.logResults;
//-- Do the fit. Because of the option above, will also print results to log
Fit.doFit("Gaussian", profileX, profileY);
//-- Optionally plot the points and curve for quality control
//Fit.plot;
//-- Save out the data
selectWindow ("Log");
saveAs ("text", dir1 + File.separator + name+".txt");
print("\\Clear");
}
run("Close All");
```