フィルターのクリア

Extract specific rows or columns containing letters (xxx) From dat File in Matlab

3 ビュー (過去 30 日間)
victor Mwarumba
victor Mwarumba 2018 年 6 月 11 日
編集済み: Paolo 2018 年 6 月 11 日
looking to come up with a script in matlab.the script should extract rows and columns containing/starting with letters xxx only in a dat file. This should be repeated for a number of DAT files. I have attached example xlsx file. for this example file I would like to extract any data that has the letters NSV. Any assist is welcomed. thanks in advance.
  5 件のコメント
victor Mwarumba
victor Mwarumba 2018 年 6 月 11 日
yes it was the matlab version works on R2016 but not R2013. yes i mean from each time step there is a corresponding NSV data which I would like to extract.
Paolo
Paolo 2018 年 6 月 11 日
@victor I have submitted an answer to your question. The output data contains the information you require. I also explain how you can programmatically change the combination of characters the code looks for. If the problem has been solved you can mark the question as accepted.

サインインしてコメントする。

採用された回答

Paolo
Paolo 2018 年 6 月 11 日
編集済み: Paolo 2018 年 6 月 11 日
The code below reads the xlsx file as a table t using readtable and converts it to a cell array C with table2cell . It uses cellfun and regexp to determine which cells in C contain 'NSV'. Find is used to determine the row and column of each entry in tokens in the cell array C.
If you need to match three different combinations of letters, replace 'NSV' in the regexp with said combination. If you need to do it programmatically, you can use strcat to obtain the expression string used for the regexp.
t = readtable('dta.xlsx','ReadVariableNames',false);
C = table2cell(t);
[tokens,matches] = cellfun(@(x) (regexp((x),'(.+)(?<=NSV)(.+)','match','tokens')),C,'un',0);
[row,col] = find(cellfun(@(x) ~isempty(x),tokens));
data = C(row:end,col);
The first row of data is the char vectors which contain 'NSV', where the following rows contain all the corresponding data.
Sample output, first column of data:
'BCS_PHY/FUN_OUT/ENV/HYD_NSV1_COIL1_CURRENT_OVERRIDE_OUTPUT'
'0'
'0'
'0.012293'
'0.012293'
'-0.024586'
...
...
...
Sample output, second column of data:
'BCS_PHY/FUN_OUT/ENV/HYD_NSV1_COIL2_CURRENT_OVERRIDE_OUTPUT'
'0.012305'
'0'
'0.012305'
'0'
'-0.024611'
...
...
...
  2 件のコメント
victor Mwarumba
victor Mwarumba 2018 年 6 月 11 日
thank you that's what I wanted hope the readtable will work for .dat file ?
Paolo
Paolo 2018 年 6 月 11 日
編集済み: Paolo 2018 年 6 月 11 日
Should work fine, .dat is supported by readtable.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by