Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 使用textscan读取字符串中的子字符串和数字时的空白单元格_Matlab_Textscan_File Import - Fatal编程技术网

Matlab 使用textscan读取字符串中的子字符串和数字时的空白单元格

Matlab 使用textscan读取字符串中的子字符串和数字时的空白单元格,matlab,textscan,file-import,Matlab,Textscan,File Import,我有一个文本文件,它由一行接一行的数据组成,格式类似xml,如下所示: <item type="newpoint1" orient_zx="0.8658983248810842" orient_zy="0.4371062806139187" orient_zz="0.2432245678709263" electrostatic_force_x="0" electrostatic_force

我有一个文本文件,它由一行接一行的数据组成,格式类似xml,如下所示:

<item type="newpoint1" orient_zx="0.8658983248810842" orient_zy="0.4371062806139187" orient_zz="0.2432245678709263" electrostatic_force_x="0" electrostatic_force_y="0" electrostatic_force_z="0" cust_attr_HMTorque_0="0" cust_attr_HMTorque_1="0" cust_attr_HMTorque_2="0" vel_x="0" vel_y="0" vel_z="0" orient_xx="-0.2638371745169712" orient_xy="-0.01401379799313232" orient_xz="0.9644654264455047" pos_x="0" cust_attr_BondForce_0="0" pos_y="0" cust_attr_BondForce_1="0" pos_z="0.16" angvel_x="0" cust_attr_BondForce_2="0" angvel_y="0" id="1" angvel_z="0" charge="0" scaling_factor="1" cust_attr_BondTorque_0="0" cust_attr_BondTorque_1="0" cust_attr_BondTorque_2="0" cust_attr_Damage_0="0" orient_yx="0.4249823952954215" cust_attr_HMForce_0="0" cust_attr_Damage_1="0" orient_yy="-0.8993006799250595" cust_attr_HMForce_1="0" orient_yz="0.1031903618333235" cust_attr_HMForce_2="0" />
expression = '"[-+]?\d*\.?\d*"';
expression2 = '"\w*?"';

newStr = regexprep(firstline,expression,'"%f"');
FormatString = sprintf('%s',regexprep(newStr,expression2,'"%s"'));
我通过以下调用重新打开文件以读取带有字符串的文件:

while ~feof(InputFile) % Read all lines in file    
    data = textscan(InputFile,FormatString,'delimiter','\n');    
end
但我得到的只是一个空单元格数组。我看不出我的错误是什么——有人能给我指出正确的方向吗

澄清: Mathworks为textscan提供了以下示例,以删除文本,这就是我正在尝试做的

“从上一示例中数据第二列的每个字段中删除文字文本‘Level’。”


好的,今天我用新的眼光看了一下,发现了我的问题

newStr = regexprep(firstline,expression,'"%f"');
FormatString = sprintf('%s',regexprep(newStr,expression2,'%q'));

data = textscan(InputFile,FormatString,'delimiter',' ');

字符串的替换需要切换到%q选项,该选项允许读取双引号内的字符串,并且textscan的分隔符需要还原为单个空格。代码现在可以正常工作。

regexp(文本,“(.*?”,“匹配”)‍‍‍‍‍‍ ‍‍ ‍‍‍‍‍‍? ‍‍ ‍‍‍‍‍‍ ‍‍ ‍‍‍‍‍‍ ‍‍ ‍‍‍‍‍‍ ‍‍这并没有告诉我为什么我的格式字符串不能用于文本扫描。虽然这是另一种可能的方法,但我只是想给你们一个不同的解决方案+1.
newStr = regexprep(firstline,expression,'"%f"');
FormatString = sprintf('%s',regexprep(newStr,expression2,'%q'));

data = textscan(InputFile,FormatString,'delimiter',' ');