MATLAB: How use of sprintfunction to load .dat file, mathematical operation, save as.txt file in a loop

for loopsprintfunction

Dear all,
I have to take several thousand files, load them or open them in matlab, multiply one of the columns by a constant value. For this i would like to use a loop, my thought was to best use the sprintfunction. SO far so good. However already in trying to load the data i get stuck. I ran into this problem a couple of times and woudl really appreciate if someone could help me 🙂
The files are double column files, and the format is .dat. If i load them individually that is no problem but somehow when i use the sprintfunction it does not work.
What i tried
1) load('C:\Desktop\waxs_import_export\e16265_1_01729_00000_00191.dat') %works fine
then
2) for ii=191:192
stemp = sprintf('C:\Desktop\waxs_import_export\e16265_1_01729_00000_00%d.dat', ii)
load(stemp)
end
the problem seems to be that it cannot read the file:
Unable to read file 'C:\Desktop\waxs_import_export'. Input cannot be a directory
does anybody know which sacred matlab rule i violated this time, and help me?
Thank you very much

Best Answer

I was surprised that your load command actually worked, so I took a look at the documentation. Turns out you can put in any ASCII file. I keep learning every day.
Now it is your turn to have a look at the documentation, for sprintf specifically, either by typing doc sprintf, or by following this link. Then take look at the "Text Before or After Formatting Operators" section. There you will notice that the backslash has a special meaning and should be escaped, resulting in the line of code below.
stemp=sprintf('C:\\Desktop\\waxs_import_export\\e16265_1_01729_00000_00%d.dat', ii)
PS you may want to consider replacing '_00%d' by '_%05d' to ensure the correct number of zeros will be used if you enter lower or higher values.