-1

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?

  • Please see [mcve]. `fprintf('%s', '12, %2 (m) %Z ---')` works as expected. – sco1 Oct 13 '17 at 19:45
  • Please look at the above link again. You have missed the **complete** and **verifiable** portions of the topic. It is completely unclear how you are actually defining `dataString`. – sco1 Oct 13 '17 at 20:34
  • ***1.*** String arrays in MATLAB use double quotes `" "`. What you have is a character array. ***2.*** As excaza said, it is not clear how you are defining `dataString` – Sardar Usama Oct 13 '17 at 21:11
  • I have added more about where dataString comes from, but its exact origin is irrelevant. What matters is its data type, contents, and what I want to do with the contents. A solution for the character array I wrote in my question will suffice to solve the general case that I need to address. – Unique Worldline Oct 13 '17 at 21:12
  • @Sardar Usama, You are correct. I apologize. it is a character array. Please see the comment above however. – Unique Worldline Oct 13 '17 at 21:13
  • An MCVE can be executed by copying pasting it as it is. `dataString` that you've provided is not copy-pasteable – Sardar Usama Oct 13 '17 at 21:17
  • To make it an MCVE, you'd probably need [***this***](https://pastebin.com/Wpgc4VDB) as `dataString`. – Sardar Usama Oct 13 '17 at 22:08

1 Answers1

1

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
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
  • @UniqueWorldline You're welcome. Please also consider editing the question in the form I provided to you in the comments and remove extra/un-needed data to make your question more useful for the future visitors as well – Sardar Usama Oct 14 '17 at 18:40