To link string of one .txt file to a value of another?

1 view (last 30 days)
I have 2 .txt files
One with a value and a string (class) and one with values.
Example: 1.txt
1 "classCar"
2 "classBicycle"
3 "classFoot"
Example: 2.txt
10 1
20 2
30 3
How can I get the value linked with "classBicycle" and compare it with 2.txt to get the value with 2 (20)?
Thanks for the help!

Accepted Answer

chrisw23
chrisw23 on 7 Sep 2022
defLines = readlines("1.txt").replace("""","").split;
rawLines = readlines("2.txt").split;
defTbl = array2table(defLines,"VariableNames",["classId" "className"]);
rawTbl = array2table(rawLines,"VariableNames",["value" "classId"]);
resStruct.className = rawTbl.classId.replace(defTbl.classId,defTbl.className);
resStruct.value = rawTbl.value;
resStruct.classId = rawTbl.classId;
struct2table(resStruct) % linked for access
another option would be the use of an enumeration class

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!