Writing Results of a Curve Fit to a Notebook


// SendGaussianFitResultsToNB(nb, comment, dataWave, W_coef, W_sigma)
// Demonstrates writing the results of a Gaussian curve fit to a notebook.
// To try it, paste the functions below into the procedure window of a new experiment.
// Now execute this on the command line:
//   Demo()
Function SendGaussianFitResultsToNB(nb, comment, dataWave, W_coef, W_sigma)
	String nb				// Name of notebook, e.g., "Notebook0"
	String comment			// Comment to be added to the notebook
	Wave dataWave			// The wave that was fit
	Wave W_coef			// Output from the CurveFit operation
	Wave W_sigma			// Output from the CurveFit operation
	
	// If notebook does not exist, create it
	if (WinType(nb) == 0)
		NewNotebook /N=$nb /F=1
	endif
	
	String text = ""
	String temp
	
	text += "\r"				// Blank line to separate from previous run
	text += comment + "\r"
	
	// Handle y0 coefficient
	sprintf temp, "y0 = %g +/- %g\r", W_coef[0], W_sigma[0]
	text += temp
	
	// Handle amplitude coefficient
	sprintf temp, "A = %g +/- %g\r", W_coef[1], W_sigma[1]
	text += temp
	
	// Handle x0 coefficient
	sprintf temp, "x0 = %g +/- %g\r", W_coef[2], W_sigma[2]
	text += temp
	
	// Handle width coefficient
	sprintf temp, "width = %g +/- %g\r", W_coef[3], W_sigma[3]
	text += temp
	
	// Insert text in notebook
	Notebook $nb, text = text
End

Function Demo()
	// Create data
	Make/O/N=100 gaussian
	
	// Set X scaling
	SetScale x -3, 3, gaussian
	
	// Fill with sample data
	Wave gaussian
	gaussian = exp(-x^2) + gnoise(.1)
	
	// Display in graph
	DoWindow/F GaussianGraph
	if (V_flag == 0)								// Graph does not yet exist
		Display /N=GaussianGraph gaussian
	endif
	
	// Do the curve fit
	CurveFit gauss gaussian /D
	Wave W_coef, W_sigma						// These are outputs from the fit
	
	// Change the color of the destination wave
	ModifyGraph rgb(fit_gaussian) = (0, 0, 65535)
	
	// Add results to the notebook (after creating it if necessary)
	String comment = "Fit performed on " + date() + " at " + time()
	SendGaussianFitResultsToNB("FitResultsNotebook", comment, gaussian, W_coef, W_sigma)
End

Forum

Support

Gallery

Igor Pro 10

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More