Efficient way of extracting parameters from a text file without external utilities
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Wander11
am 9 Jul. 2019
Beantwortet: Abhilash Padma
am 17 Jul. 2019
I have a txt file whose content is as follows (There are around 1000,000 rows).
Basically there are 4 parameters, each of which can take non-negative integer values:
- type 0,val1
- type 0,val2
- type 1,val1
- type 1,val2
I need to extract their values and form 4 arrays one for each parameters. . How can I do this in a time efficient way?
1 Kommentar
Walter Roberson
am 9 Jul. 2019
This belongs as part of https://www.mathworks.com/matlabcentral/answers/470798-fastest-way-to-create-arrays-programmatically-for-particular-parameter-values-from-a-set-of-text-fi
Akzeptierte Antwort
Abhilash Padma
am 17 Jul. 2019
Hi,
I understand that there is a text file which contains different values belongs to any of the 4 types.
They are:
type 0, val1 ; type 0, val2 ; type 1, val1 ; type 1, val2
You need to read all the values and store them in 4 variables where each variable belongs to different datatypes.
Try the following code, which should work for your problem.
str=fileread('test.txt');
var1=str2double(regexp(str, '(?<=type\s+0,val1\s+=\s+)\w*', 'match'));
var2=str2double(regexp(str, '(?<=type\s+0,val2\s+=\s+)\w*', 'match'));
var3=str2double(regexp(str, '(?<=type\s+1,val1\s+=\s+)\w*', 'match'));
var4=str2double(regexp(str, '(?<=type\s+1,val2\s+=\s+)\w*', 'match'));
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!