我在matlab脚本中有一个名为“dataString”的字符串数组,它使用fileread()从html文档复制到MATLAB中。然后我删除了我想要的部分并将其存储在dataString中。
TEXT = fileread(htmldocument);
k1 = strfind(TEXT,stringDelimiter1)
k2 = strfind(TEXT,stringDelimiter2)
dataString(:) = TEXT(k1(1):(k1(1) - 1))
然后对其内容进行过滤,使其不包含html代码,但除了字母外,它还可以包含特殊字符和数字。下面的dataString内容的解决方案应该足以满足我试图解决的问题的一般情况。 dataString中包含各种字符和数字,并且在文本中具有特定点,其中在MATLAB中打印时回车是可见的。如果我要求matlab在命令窗口中打印它,它会自行格式化:
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.
我希望能够使用fopen,fprintf和fclose来获取dataString的内容并将它们放在文本文件'genericTextFileName.txt'中 当我在MATLAB中打印dataString时,在每行打印的字符也打印在文本文件的后续行中。当我执行以下操作时:
fileDirectory = 'C:\Users\UniqueWorldline\Desktop'
[fid, errorMsg] = fopen(fileDirectory, 'w')
byteCount = fprinf(fid, '%s', dataString)
fcloseFile = fclose(fid)
dataString打印到文本文件中,如下所示:
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.'
基本上,dataString中存在的所有新行或回车格式都将丢失。删除'%s'没有帮助,因为fprintf然后认为'%'是一个特殊字符,我不能这样做,因为它会在第一个'%'之后删除所有内容。我需要这种格式存在于文本文件中。在阅读了人们对fprintf和函数本身的文档的许多其他相关问题之后,我无法找到问题的答案。我怎么能这样做?
答案 0 :(得分:1)
您提到的问题是操作系统和编辑器特定。通常,Windows中的编辑器(如记事本)需要带有换行符\r
的回车符\n
。如果你在notepad ++中打开文件,你确实会看到新行,就像在MATLAB的命令窗口中一样。
有关详细说明,请阅读以下文章:
Difference between \n and \r?
对于您的编辑器,如documentation中所述,您需要使用文本模式,在输出中的所有\r
之前插入\n
,同时使用{打开文件{1}}。即。
fopen