Load Matlab 1D Numeric Text File


Menu "Load Waves"
	"Load Matlab 1D Numeric Text File...", LoadMatlab1DNumericTextFile("", "")
End

// LoadMatlab1DNumericTextFile(pathName, fileName)
// 
// Loads a .m file containing one or more lines of numeric text data in this format:
// 	<name> = [number<space>number<space>...]
// 
// Data can be exported in this format from Matlab using this Matlab command:
//	save my_data.out <array1> -ASCII
//	save my_data.out <array2> -ASCII -append
// 
// Each line is loaded into a separate 1D wave which is named based on <name>.
// 
// pathName is an Igor symbolic path.
// If either pathName or fileName is "", an Open File dialog is presented.
// 
// The function returns a semicolon-separated string containing the names
// of the loaded waves.
// 
// NOTE: Any pre-existing waves with the same names are overwritten.
// 
// NOTE: This works for 1D NUMERIC data only.

Function/S CreateWaveFromMFileLine(text)
	String text
	
	Variable startPos, endPos
	
	// Find name
	startPos = 0
	endPos = strsearch(text, " ", startPos)		// Look for first space
	if (endPos < 0)
		Print "CreateWaveFromMFileLine unable to find wave name"
		return ""								// Error
	endif
	String name = text[startPos,endPos-1]
	
	name = CleanupName(name, 0)				// Make sure it is a legal Igor name
	
	startPos = strsearch(text, "[", endPos)		// Look for left bracket
	if (endPos < 0)
		Printf "CreateWaveFromMFileLine unable to find left bracket for wave %s\r", name
		return ""								// Error
	endif
	
	// Replace spaces with CRLF and write the resulting text to a file so it can be loaded by LoadWave
	String tempFilePath = SpecialDirPath("Temporary", 0, 0, 0) + "TempLoadMatlab1DNumericTextFile.txt"
	String tempData = ReplaceString(" ", text, "\r\n")
	Variable tempRefNum
	Open tempRefNum as tempFilePath
	FBinWrite tempRefNum, tempData
	Close tempRefNum
	
	// Now load the data as general text
	String columnInfoStr = "N=" + name + ";"		// e.g., N=wave0;
	LoadWave /O /D /G /A /B=columnInfoStr /Q tempFilePath
	
	DeleteFile /Z tempFilePath
	
	return name	
End

Function/S LoadMatlab1DNumericTextFile(pathName, fileName)
	String pathName		// Name of Igor symbolic path or "" for dialog
	String fileName			// Name of file
	
	Variable numLines = 0
	
	Variable refNum
	
	String fileFilters = "M Files (*.m):.m;"
	fileFilters += "All Files:.*;"
	Open/R/P=$pathName/F=fileFilters refNum as fileName
	if (strlen(S_fileName) == 0)
		return ""			// User cancelled
	endif
	
	String nameList = ""
	
	do
		String text
		FReadLine refNum, text
		if (strlen(text) == 0)
			break
		endif
		
		String name = CreateWaveFromMFileLine(text)
		if (strlen(name) == 0)
			Printf "LoadMatlab1DNumericTextFile unable to load wave from line %d\r", numLines
		else
			nameList += name + ";"
		endif
		
		numLines += 1
	while(1)
	
	Close refNum
	
	return nameList
End

I would like to load Matlab waves into Igor. I'm doing EEG work and have 32 channels of data that I would like to analyzed in Igor.
The first problem I am having is with the Matlab load XOP. I already found out that there is a problem with this. I tried as suggested to change the extension to "anything else", but that did not help.

As for the code that Howard posted here, it does not appear that this would work for 32 channels of EEG data. But perhaps I'm wrong.

Could anyone please provide guidance on how to proceed?

thanks


Chuck Larson

Forum

Support

Gallery

Igor Pro 10

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More