<分区>
如何在 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 下,并将数据存储在字母 n 下在数组 2 中)。
有什么帮助吗?
<分区>
如何在 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 下,并将数据存储在字母 n 下在数组 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 - 在 MATLAB 中解析文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15750351/