在MATLAB中解析文本文件 [英] parse text file in MATLAB
本文介绍了在MATLAB中解析文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在MATLAB中解析文件?文本中的数据具有以下格式:
How can I parse file in MATLAB? The data in the text has this format:
p
15.01245 20.478
12.589 58.256
n
16.589 87.268
52.367 46.256
2.589 58.02
我想将每个数据存储在 单独的数组 ( ie 中;将数据存储在字母p下的数组1中,并将数据存储在字母n下在数组2中).
I want to store each data in separate array (i.e; store data under letter p in array 1, and data under letter n in array 2).
有什么帮助吗?
推荐答案
这是另一种解决方案:
fstring = fileread('test.txt'); % read the file as one string
fblocks = regexp(fstring,'[A-Za-z]','split'); % uses any single character as a separator
fblocks(1) = []; % removes anything before the first character
out = cell(size(fblocks));
for k = 1:numel(fblocks)
out{k} = textscan(fblocks{k},'%f %f','delimiter',' ','MultipleDelimsAsOne', 1);
out{k} = horzcat(out{k}{:});
end
这篇关于在MATLAB中解析文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文