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

1 vue (au cours des 30 derniers jours)
Corneel Lootens
Corneel Lootens le 7 Sep 2022
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!

Réponse acceptée

chrisw23
chrisw23 le 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

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by