Using fprintf for .txt file
Show older comments
Hi All,
I'm having a bit a trouble trying to figure out this problem using fprintf to create a .txt input file to some analysis software I am using. This software suite is very particular with the format of this text file and I beleieve I have everything correct. The problem is that the sofware will not read the file, unless I open the text file after creating it with fprintf and press save. No additions to the file are made, and the date modified does not change either... Any thoughts?
Thanks, Marc
Accepted Answer
More Answers (5)
Kelly Kearney
on 5 May 2011
0 votes
Possibly endline issues? I know a lot of software is picky about \n vs \r vs \r\n, and you may be modifying that when you resave the file in a text editor. Just a guess...
2 Comments
Fangjun Jiang
on 5 May 2011
Good point. I guess you can do a comparison. One is the file created by fprintf() in Matlab. The other is the one saved right after you opened it.
Marc
on 5 May 2011
Fangjun Jiang
on 5 May 2011
0 votes
It sounds like a tricky problem. I once had an experience that an EOF (end of file) character makes or breaks my tool chain.
One quick thing to try, after you created the text file using fprintf(), go to Windows Command window, use the copy command to create two files;
copy Original.txt text1.txt
copy /b Original.txt text2.txt
See if you downstream software accepts text1.txt, or text2.txt
3 Comments
Marc
on 5 May 2011
Fangjun Jiang
on 5 May 2011
How did you use fopen() in your script? It's good practice to specify it as text file, for example:
FID=fopen('Original.txt','w+t');
Marc
on 5 May 2011
Marc
on 5 May 2011
0 votes
5 Comments
Jason Ross
on 5 May 2011
You can likely just do it from the DOS prompt using one of the ways I mentioned above, combined with the "for" command.
If you type "help for" at a DOS command there are some examples of how to run the same thing on a bunch of files. I think one way would end up looking like the following:
for %i IN (*.txt) do dos2unix %i
This would get all the *.txt files in a directory and then run the dos2unix command on it, which would strip the control-m's out.
You might need to tweak the above a little to make it work, and I'd also suggest experimenting with a few files in a temp directory first.
Marc
on 5 May 2011
Jason Ross
on 5 May 2011
The solution I proposed was for your existing files.
To fix your root cause, since you've already tried \n, \n\r and \r\n, then you might need to find the hex code for the "correct" type of line ending and use it. See
http://www.mathworks.com/help/techdoc/ref/fprintf.html
For how to insert that hex code in your fprintf.
Jason Ross
on 5 May 2011
Oh, and of course \r alone.
From the above doc, it seems that to get correctly formatted text in Notepad you need to use "\r\n" ... but it looks like you already tried that.
Fangjun Jiang
on 5 May 2011
Reading the help for fopen() regarding the 't' permission, would it be worth to try fopen('Original.txt','w')?
If the file is open in binary mode, and your fprintf() is using '\n' (new line) alone, the carriage return won't be added.
You can open files in binary mode (the default) or in text mode.
In binary mode, no characters get singled out for special treatment.
In text mode on the PC, the carriage return character preceding
a newline character is deleted on input and added before the newline
character on output. To open a file in text mode, append 't' to the
permission string, for example 'rt' and 'w+t'. (On Unix, text and
binary mode are the same, so this has no effect. On PC systems
this is critical.)
Fangjun Jiang
on 5 May 2011
In your script, you can open the file but I don't know how to close it.
edit('Original.txt');
system('notepad Original.txt');
Maybe you can try the copyfile() function in Matlab to see if the copied file makes a difference.
Did you check the fopen() function in your script has the text specification? like
FID=fopen('Original.txt','w+t')
Categories
Find more on Language Support in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!