필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
Dorna
Dorna 2023년 2월 17일
답변: Stephen23 2023년 2월 17일
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
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일
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일
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 "" ""

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by