Filter löschen
Filter löschen

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

4 Ansichten (letzte 30 Tage)
Dorna
Dorna am 17 Feb. 2023
Beantwortet: Stephen23 am 17 Feb. 2023
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 Kommentare
Dorna
Dorna am 17 Feb. 2023
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 am 17 Feb. 2023
Bearbeitet: Stephen23 am 17 Feb. 2023
@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.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Oguz Kaan Hancioglu
Oguz Kaan Hancioglu am 17 Feb. 2023
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 am 17 Feb. 2023
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 "" ""

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by