Using fprintf for .txt file
7 views (last 30 days)
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
0 Comments
Accepted Answer
Jason Ross
on 5 May 2011
What program are you opening it with? (I assume MATLAB, but it could also be Word, Notepad, Wordpad, etc). I've had experiences where a given editing program might change the format when you save it, and also been down this rabbit hole where the slight variations of "just a text file" started to matter.
I'd suggest getting a text editor that can show all the formatting characters and look at the files using that. I'm a Vim fan, but emacs and a host of others can do it, too. Look at the before and after and you might be able to get what it's expecting at the end of the file.
You should also take a look at the tool you are using for comparing and make sure you make the comparisons very strict. A lot of comparison/diff tools ignore things like carriage returns and whitespace and concentrate on the actual content, but offer options to turn on this checking if you need it.
Good luck!
3 Comments
Jason Ross
on 5 May 2011
Ahhh, the old control-m! How I haven't missed you.
There are a few ways to do this, as this has existed for a while. If you google for "remove control m", you can get a lot of approaches to the problem.
- Use a utility like dos2unix, which will strip out the control-m's. There are many free versions available.
- You can use a perl command to search and replace it (google for syntax)
- If you have a UNIX like toolkit installed (MKS, Cygwin), you can do it with tr or sed
More Answers (5)
Kelly Kearney
on 5 May 2011
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.
Fangjun Jiang
on 5 May 2011
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
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
5 Comments
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')
See Also
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!