Search code examples
stringmatlabprintf-debugging

Ignoring special characters while preserving formatting with MATLAB's fprintf function


I have a string array in a matlab script called "dataString" that was copied into MATLAB from an html document using fileread(). I then cut out the part I wanted and stored that in dataString.

TEXT = fileread(htmldocument);
k1 = strfind(TEXT,stringDelimiter1)
k2 = strfind(TEXT,stringDelimiter2)
dataString(:) = TEXT(k1(1):(k1(1) - 1))

Its contents were then filtered so that it does not contain html code, but it still can contain special characters and numbers in addition to letters. A solution for the contents of dataString below should suffice for the general case of the problem I am trying to solve. dataString has a variety of characters and numbers in it, and has specific points in the text where carriage returns are visible when printed in MATLAB. If I ask matlab to print it in the Command Window, it formats itself like this:

dataString =

'This is where the body of dataString goes.

There are not the same number of characters on

every line. Notice that MATLAB knows that there are

carriage returns interspersed throughout this text and

formats the output appropriately.

There are also numbers and other

types of characters in this array like 12, and %2

(m) %Z --- . When asked to print to the command window, MATLAB

treats all the contents of dataString as I want it to.

These contents need to be considered as generic as possible.

'

I want to be able to use fopen, fprintf, and fclose to take the contents of dataString and put them in a text file 'genericTextFileName.txt' with the same characters that print out on each line when I print dataString in MATLAB also printed on subsequent lines in the text file. When I do the following:

fileDirectory = 'C:\Users\UniqueWorldline\Desktop'
[fid, errorMsg] = fopen(fileDirectory, 'w')
byteCount = fprinf(fid, '%s', dataString)
fcloseFile = fclose(fid)

dataString is printed into the textfile like so:

dataString =

'This is where the body of dataString goes. There are not the same number of characters on every line. Notice that MATLAB knows that there are carriage returns interspersed throughout this text and formats the output appropriately. There are also numbers and other types of characters in this array like 12, and %2 (m) %Z --- . When asked to print to the command window, MATLAB treats all the contents of dataString as I want it to. These contents need to be considered as generic as possible.'

Basically, all the new line or carriage return formatting that exists in dataString is lost. Removing '%s' doesn't help since fprintf then considers '%' to be a special character, which I can't have it do because it cuts everything after the first '%'. I need this formatting to exist in the text file. After reading many other related problems people have had with fprintf and the documentation for the function itself, I cannot find an answer to my problem. How can I do this?


Solution

  • Why you're getting unexpected output:

    The problem that you've mentioned is OS and editor specific. Usually editors in Windows, like notepad, require the carriage return character \r with the new-line character \n. If you open the file in notepad++, you'll indeed see the new lines just like in MATLAB's command window.

    For more explanation, read this post:
    ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ Difference between \n and \r?


    Solution:

    For your editor, as mentioned in the documentation, you need to use the text mode, to insert a \r before all \n in the output, while opening the file with fopen. i.e.

    [fid, errorMsg] = fopen('file.txt', 'wt');   %Notice wt