How to read a specific value next to a text in a text file?

Hi everyone,
I am using MATLAB to read a specific file and I need to extract information. The format is as follows
Value (any number) - / text here (A)
Value (any number) - / text here (B)
I would like to know how to extract all the value for the "- / text here (B)". Thanks for your help in advance.

4 件のコメント

Stephen23
Stephen23 2023 年 2 月 17 日
@Dorna: please upload a sample data file by clicking the paperclip button.
Dorna
Dorna 2023 年 2 月 17 日
@Stephen23 here it is:
Dorna
Dorna 2023 年 2 月 17 日
and by the way, how can I store it based on Zone names (Zone A and Zone B) or write it in a way that a specific Zone coordinates are replaced with specific coordiantes.
Stephen23
Stephen23 2023 年 2 月 17 日
編集済み: Stephen23 2023 年 2 月 17 日
@Dorna: Do you know the name of the application that generated that file, or name of the file format? Someone may have already written a file parser for that file format.

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

回答 (2 件)

Oguz Kaan Hancioglu
Oguz Kaan Hancioglu 2023 年 2 月 17 日

0 投票

I understand your data format is text-based as you want to read or write both numbers and strings. If all elements of the data are separated using special delimiters, you can use the readtable command. Than you can find your txt B using string commands and reach the value.
clc; clear;
fileName = 'New Text Document.txt'
fileName = 'New Text Document.txt'
T = readtable(fileName,'Format','auto');
disp(T);
Var1 Var2 ______ _______ 3.1415 {'pi' } 6.2832 {'2pi'}
bIndex = find(strcmp(string(T.Var2),'2pi')==1);
disp(double(T.Var1(bIndex)))
6.2832
readtable supports many file formats including .xls, .xml, .docx, .html.
https://www.mathworks.com/help/matlab/ref/readtable.html
Stephen23
Stephen23 2023 年 2 月 17 日

0 投票

raw = strtrim(readlines('Txt.txt'));
idx = strcmp(raw,'')|strcmpi(raw,'Zone,');
raw(idx) = [];
% split the values and keys:
spl = regexp(raw,'[,;]\s*!-\s*','split','once');
spl = vertcat(spl{:});
% convert to table:
idy = startsWith(spl(:,1),'Zone');
idz = 1+cumsum(idy);
zab = ["";spl(idy,1)];
spl(:,3) = zab(idz);
tbl = array2table(spl(~idy&idz>1,:), 'VariableNames',{'Value','Key','Name'})
tbl = 24×3 table
Value Key Name _____ ___________________________________ ________ "0" "Direction of Relative North {deg}" "Zone A" "0" "X Origin {m}" "Zone A" "0" "Y Origin {m}" "Zone A" "0" "Z Origin {m}" "Zone A" "" "Type" "Zone A" "" "Multiplier" "Zone A" "" "Ceiling Height {m}" "Zone A" "" "Volume {m3}" "Zone A" "" "Floor Area {m2}" "Zone A" "" "Zone Inside Convection Algorithm" "Zone A" "" "Zone Outside Convection Algorithm" "Zone A" "Yes" "Part of Total Floor Area" "Zone A" "0" "Direction of Relative North {deg}" "Zone B" "0" "X Origin {m}" "Zone B" "0" "Y Origin {m}" "Zone B" "0" "Z Origin {m}" "Zone B"
% unstack to a more useful arrangement:
tbl = unstack(tbl,'Value','Key', 'VariableNamingRule','preserve');
tbl = convertvars(tbl,@(s)all(~isnan(double(s))),'double')
tbl = 2×13 table
Name Ceiling Height {m} Direction of Relative North {deg} Floor Area {m2} Multiplier Part of Total Floor Area Type Volume {m3} X Origin {m} Y Origin {m} Z Origin {m} Zone Inside Convection Algorithm Zone Outside Convection Algorithm ________ __________________ _________________________________ _______________ __________ ________________________ ____ ___________ ____________ ____________ ____________ ________________________________ _________________________________ "Zone A" "" 0 "" "" "Yes" "" "" 0 0 0 "" "" "Zone B" "" 0 "" "" "Yes" "" "" 0 0 0 "" ""

カテゴリ

ヘルプ センター および File ExchangeText Data Preparation についてさらに検索

製品

リリース

R2022b

質問済み:

2023 年 2 月 17 日

回答済み:

2023 年 2 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by