
Loading the 2D data as a matrix with its file name as the wave name

maniraj
I have about 50 ASCII file in a folder which are 2D data with size 1040x1376, and I could load them (at present one by one) as a matrix using following command:
LoadWave/G/M/D/A=wave ""
In this code, /A flag automatically name the wave.
But, I would like to name the wave by its file_name.
For the same, I try to use /W flag. But, did not work. It appears that with /G/M, I may not able to use /W.
Is there any in-build code that would name the wave by its file_name and also load all ASCII files in a folder.
I thank you in advance for your help.
Regards,
Maniraj
http://www.igorexchange.com/node/5843
As noted in the comments, you may have to tweak the LoadWave command to fit your file format.
July 3, 2014 at 08:54 am - Permalink
I have changed the file extension and then could load the all files which are having same file extension, successfully.
However, I am facing following problem:
If file name starts with a number, for example,
01_Test
02_Test
03_Test
....
Then, the filename automatically gets "X" as prefix and moreover, the data file format (.dat, .acs, ..) in the suffix (as shown below) !
X01_Test_dat0
X02_Test_dat0
X03_Test_dat0
....
I have tried for both .dat and .acs and the issues are same for both the cases.
If file name starts with any Alphabets, then there is no prefix "X" but suffix
I could locate source for 0 in the suffix of filename which is 0 in the below line
matrixName = UniqueName(matrixName, 1, 0)
But, I exactly could not locate the reason why file format is added and why X is added in prefix.
Please let me know if you have any solution for it.
Thanks you in advance for you help.
July 4, 2014 at 04:09 am - Permalink
DisplayHelpTopic("Object Names")
They must start with an alphabetic character.
Hope this helps,
Kurt
July 4, 2014 at 04:44 am - Permalink
When we rename a wave with number as starting character, it works!
When I try to give base name in /A=0123, then also it works!
I could command out (marked by ***) some of the lines in the original code and manage to get number as starting character, but I am still struggling to get rid of data file format at suffix of filename.
The modified code is
Function LoadAllMatrixFilesFromFolder(pathName, extension, makePlots)
String pathName // Name of an Igor symbolic path or "" to get a dialog
String extension // File name extension - e.g., "txt" or ".dat"
Variable makePlots // 1 to display each matrix in a plot
if (strlen(pathName) == 0)
NewPath/O/M="Choose a folder containing Matrix Files" LoadIndexedMatrixPath
if (V_flag != 0)
return -1 // User cancelled
endif
pathName = "LoadIndexedMatrixPath"
endif
String fileName
Variable index
index = 0
do
fileName = IndexedFile($pathName, index, extension)
if (strlen(fileName) == 0)
break // No more files.
endif
String matrixName =fileName// CleanupName(fileName, 0) ***
//Print matrixName ***
//if (exists(matrixName) != 1) ***
// matrixName conflicts with some other Igor name so make it unique ***
//matrixName = UniqueName(matrixName, 1, 5) ***
//endif ***
String columnInfoStr
sprintf columnInfoStr, "N='%s';", matrixName
LoadWave/G/D/P=$pathName/M/B=columnInfoStr fileName ///original line for .dat file format
Wave matrix = $matrixName // Create a wave reference
if (makePlots)
NewImage matrix
endif
index += 1
while (1)
End
July 4, 2014 at 05:28 am - Permalink
fileName = RemoveEnding(fileName, extension)
.July 4, 2014 at 05:32 am - Permalink
When I used below line (as you have suggested), it worked nicely.
String matrixName= RemoveEnding(fileName, extension)
Thanks a lot.
July 4, 2014 at 06:28 am - Permalink
For more details look up CleanUpNames in the help file; this will also lead to more info on naming waves and other Igor objects.
July 4, 2014 at 07:20 am - Permalink
Now, I am trying to add option to read the header info and auto scale the matrix.
Header info looks as follows:
# # ImageWidth: 1376 pixel # ImageHeight: 1040 pixel # KineticEnergy: 14 eV # PassEnergy: 160 eV # EnergyScalingOffset: 3.44767 eV # EnergyScalingFactor: 0.0153488 eV/pixel # AzimuthalScalingOffset: 6.99327 deg # AzimuthalScalingFactor: -0.0134615 deg/pixel # 00002400 00001800 00004000 .......
For scaling, I need EnergyScalingOffset, .... to....AzimuthalScalingFactor. But, I would also like to load all this info and store it in wave note.
In connection to this, I saw a forum post Here).
To use the code given in Link to above pasted program, I am not able to get proper starting point. Specifically, how to assign "refNum".
I am looking for info, how to assign refNum in the above pasted code, which line.
July 8, 2014 at 07:20 am - Permalink
Open
operation. Given a path and filename, it will open the file for reading and/or writing and will returnrefNum
, this is a handle on the open file and is used for other file manipulation operations. The following is a relevant fragment from the code that you linked to:Here the file will be opened for read only ("/R" flag). So in your functioning code, feed your path and file to Open and change the remainder of the code to find the header parameters that are needed.
Hope that I have correctly understood your question.
July 8, 2014 at 08:30 am - Permalink
I have tried to use this set of line to assign refNum, but it does not work.
Since, this code open file to load data, are there any way to directly assign refNum. So, that I can start further to load lines.
To be frank, it appears to me that it is higher level programming!
July 8, 2014 at 08:45 am - Permalink
refNum is set by the Open operation:
Here is another example that shows the big picture:
http://www.igorexchange.com/node/908
Execute this and read the help to make sure you understand what an Igor symbolic path is and that you are passing a valid value for pathName:
DisplayHelpTopic "Symbolic Paths"
You can pass "" for pathName if you pass a full path for the filePath parameter. If filePath is a partial path or a simple file name then it is relative to the folder referenced by the symbolic path named by pathName. A full path starts with a volume name (e.g., "C:" on Windows or "hd" on Macintosh). A partial path starts with a separator - typically colon but could be backslash.
It is best to use colon as the separator in file paths because backslashes are escape characters in Igor. To read about this, execute this:
DisplayHelpTopic "Path Separators"
I need to know specifically what you mean by "does not work".
It might be useful if you posted your code that attempts to read header information.
July 8, 2014 at 08:58 am - Permalink
The present code is
This code works good and loaded all .asc files in the folder. However, Header reading works only for first file and then it uses same values for 2nd data file onwards. I am confused, where is the problem. Can any one help me. For model data please find the attached file, you may change values and try. Note: I have attached data in .txt format because webpage allowed .txt and I have also deleted so many points to minimize the memory.
July 9, 2014 at 07:40 am - Permalink
I discovered this using the Igor debugger. It is invaluable. To read about it, execute:
DisplayHelpTopic "The Debugger"
I rejiggered your code to make it more modular and expressive.
I changed the variable name "i" to "lineNumber" which is more expressive. I removed the declaration of the variable from the beginning of the function to just before the loop which makes it more clear where it is used. I also changed the do loop to a for loop which guarantees that lineNumber is initialized each time the loop starts.
I removed this which is not necessary. The purpose is to display an Open File dialog to choose the file but it not useful when loading all of the files in a folder and also, if you were going to do it, you would have to do it before the first use of pathName and fileName which means before the LoadWave call.
I also split the header reading out as a subroutine. This makes the code more modular, easier to understand and easier to debug, because you don't have to wade through a monolithic block of code to find what you are interested in.
Here is the resulting code:
The ReadHeader uses pass-by-reference parameters to return results to the calling routine. To read about pass-by-reference parameters, execute:
DisplayHelpTopic "How Parameters Work"
If you were reading a large number of header parameters you would want to use a structure to return the values from ReadHeader to the main function. But for your situation pass-by-reference parameters are fine.
July 9, 2014 at 09:16 am - Permalink
Special thanks to your interest and introducing me to step by step debugger tool.
I finally added Note option and completed the function.
July 10, 2014 at 03:57 am - Permalink
I am getting "Attempt to use a null string variable" error for below segment
I could not get what is missing. Any help is greatly appreciated.
November 4, 2015 at 03:05 pm - Permalink
Have you run it with the debugger turned on?
This should help you track down the issue.
HTH,
Kurt
November 4, 2015 at 10:59 pm - Permalink
November 4, 2015 at 11:42 pm - Permalink
As a starter, here is some code that tests a wavename starting with a digit...
This runs as I would expect...
Can you add to this to reproduce your error message?
HTH,
Kurt
November 5, 2015 at 12:56 am - Permalink
Are you sure the error is here:
...and not in the previous line?
November 5, 2015 at 01:13 am - Permalink
Wave matrix = $matrixName
i.e. Error is in
LoadWave/G/D/P=$pathName/M/B=columnInfoStr fileName ///original line for .dat file format
The below link helped.
http://www.igorexchange.com/node/6490
November 5, 2015 at 02:19 pm - Permalink