This question already has an answer here:
Closed 6 years ago.
Parse file in MatLab
(1个答案)
如何在MATLAB中解析文件文本中的数据具有以下格式:
p
15.01245  20.478
12.589  58.256
n
16.589  87.268
52.367  46.256
2.589  58.02

我希望将每个数据存储在单独的数组中(即,将数据存储在数组1的字母p下,将数据存储在数组2的字母n下)。
有什么帮助吗?

最佳答案

下面是另一个解决方案:

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

10-04 20:41