Using fprintf for .txt file

7 vues (au cours des 30 derniers jours)
Marc
Marc le 5 Mai 2011
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

Réponse acceptée

Jason Ross
Jason Ross le 5 Mai 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 commentaires
Marc
Marc le 5 Mai 2011
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.
Jason Ross
Jason Ross le 5 Mai 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

Connectez-vous pour commenter.

Plus de réponses (5)

Fangjun Jiang
Fangjun Jiang le 5 Mai 2011
did you close the file like fclose(FID)?
  1 commentaire
Marc
Marc le 5 Mai 2011
I have used fclose(All). Would this suffice?

Connectez-vous pour commenter.


Kelly Kearney
Kelly Kearney le 5 Mai 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 commentaires
Fangjun Jiang
Fangjun Jiang le 5 Mai 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
Marc le 5 Mai 2011
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.

Connectez-vous pour commenter.


Fangjun Jiang
Fangjun Jiang le 5 Mai 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 commentaires
Fangjun Jiang
Fangjun Jiang le 5 Mai 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
Marc le 5 Mai 2011
I used 'wt'

Connectez-vous pour commenter.


Marc
Marc le 5 Mai 2011
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 commentaires
Jason Ross
Jason Ross le 5 Mai 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
Fangjun Jiang le 5 Mai 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.)

Connectez-vous pour commenter.


Fangjun Jiang
Fangjun Jiang le 5 Mai 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')
  1 commentaire
Marc
Marc le 5 Mai 2011
I used 'wt'

Connectez-vous pour commenter.

Catégories

En savoir plus sur Language Support dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by