在结构化数组中存储文本文件 [英] Storing text file in structured array

查看:147
本文介绍了在结构化数组中存储文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构良好的输入文本文件:

I have an input text file which is well structured:

START_PARAMETERS
C:\Users\admin\Desktop\Bladed_wind_generator\_wind
C:\Users\admin\Desktop\Bladed_wind_generator\reference_v_4_2.$PJ
END_PARAMETERS
---------------------------------------------------------------------------
START_DLC1-2
4 6 8 10 12 14 16 18 20 22 24 26 28 29
6
8192
600
END_DLC1-2
---------------------------------------------------------------------------
START_DLC6-1
44.8
30
8192
600
END_DLC6-1
---------------------------------------------------------------------------
START_DLC6-4
3 31 33 35
6
8192
600
END_DLC6-4
---------------------------------------------------------------------------
START_DLC7-2
2 4 6 8 10 12 14 16 18 20 22 24 
6
8192
600
END_DLC7-2
---------------------------------------------------------------------------

目前我用这种方式阅读:

At the moment I read it this way:

clc,clear all,close all

f = fopen('inp.txt','rt');  % Read Input File
C = textscan(f, '%s', 'Delimiter', '\r\n');
C = C{1}; % Store Input File in a Cell
fclose(f);

然后,通过regexp我读取每次出现的(START_DLC / END_DLC)块:

Then, by means of regexp I read every occurrence of (START_DLC/END_DLC) block:

startIndx = regexp(C,'START_DLC','match');
endIndx = regexp(C,'END_DLC','match');

目的是在结构化单元格中的每个START_DLC / END_DLC块之间存储文本内容(应该被称为store_DLCs)。结果必须是(例如DLC1-2):

The aim is to store the content of the text between each START_DLC/END_DLC block in a structured cell(supposed to be called store_DLCs). The result has to be(e.g. DLC1-2):

DLC1-2
4 6 8 10 12 14 16 18 20 22 24 26 28 29
6
8192
600

等等,直到DLC7-2。

and so on until DLC7-2.

你能不能给我一些提示如何继续?

Would you mind to give me some hints how to proceed?

我提前感谢你们。

BR,
Francesco

BR, Francesco

推荐答案

到目前为止你的代码还可以。但有一点,我会稍微改变你对 startIndx endIndx 的计算结果如下:

Your code so far is okay. One thing though, I'd slightly alter your computation of startIndx and endIndx to the following:

startIndx = find(~cellfun(@isempty, regexp(C, 'START_DLC', 'match')));
endIndx = find(~cellfun(@isempty, regexp(C, 'END_DLC', 'match')));

这样你就可以获得实际的指数(我已将它们转换为此处视觉上方便),如下:

so that you would get actual indices (I've transposed them here for visual convenience), like so:

startIndx =

     6    13    20    27


endIndx =

    11    18    25    32

我还会添加一个断言来检查输入的完整性:

I'd also add an assertion to check the integrity of the input:

assert(all(size(startIndx) == size(endIndx)))

现在,如上所述计算所有指数,你可以继续提取数据进入单元格:

Now, with all the indices computed as above, you can proceed to extract the data into cells:

extract_dlc = @(n)({C{startIndx(n):endIndx(n) - 1}});
store_DLCs = arrayfun(extract_dlc, 1:numel(startIndx), 'UniformOutput', false)

要修复每个单元格的名称(首先是条目),你可以这样做:

And to "fix" the names (which are first entries) of each cell, you can do:

fix_dlc_name = @(x){strrep(x{1}, 'START_', ''), x{2:end}};
store_DLCs = cellfun(fix_dlc_name, store_DLCs,  'UniformOutput', false);

此代码应用于您的示例输入将产生1 x 4单元格的单元格数组:

This code applied on your example input would yield a 1-by-4 cell array of cells:

store_DLCs =

    {'DLC1-2', '4 6 8 10 12 14 16 18 20 22 24 26 28 29', '6', '8192', '600'}  
    {'DLC6-1', '44.8', '30', '8192', '600'}   
    {'DLC6-4', '3 31 33 35', '6', '8192', '600'}   
    {'DLC7-2', '2 4 6 8 10 12 14 16 18 20 22 24', '6', '8192', '600'}

这篇关于在结构化数组中存储文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆