Using fprintf for .txt file

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

Jason Ross
Jason Ross on 5 May 2011

0 votes

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

Opening/saving with notepad, but reading final product from a another software suite.
I just used Vim, and found my problem. The re-saved file shows the correct format, but the unsaved file shows "^M" where there is supposed to be a new line... how do i fix this.
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

Sign in to comment.

More Answers (5)

Fangjun Jiang
Fangjun Jiang on 5 May 2011

0 votes

did you close the file like fclose(FID)?

1 Comment

I have used fclose(All). Would this suffice?

Sign in to comment.

Kelly Kearney
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

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.
I have tried all combinations of newlines/carriage returns and such and have even used the windows cmd compare tools to see if I can find the difference. No such luck.

Sign in to comment.

Fangjun Jiang
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

Software doe snot accept those either...
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');
I used 'wt'

Sign in to comment.

Marc
Marc on 5 May 2011

0 votes

Is there anyway Matlab can open these files and save them as if I were doing if by hand in notepad. I have thousands of files to do this for...

5 Comments

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.
So there is no way to Matlab to write it with out those control values... It opens up formatted correctly in wordpad, but not notepad. Notepade just looks like one continuous line.
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.
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.
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.)

Sign in to comment.

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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!