to extract information from a text file?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Im stuck and need some help.
I need to find a specific text file in a folder with some images and extract some information from a text file. The text file has the wording "ZFocus" in its header, which will always be in the title but the text before and after can be diferent from time to time.
This text file includes a lot of lines with text, that does not look the same in every row. Some of the data I would lile to extract can look like this:
...
Exposure Time = 135 s
Area = -1,048161E-8 Vs
Amplitude = 1,800100E-1 V
Pulsewidth = 500,832700E-8 s
Attenuation = 0,000000E+0 9,151000E+1 8,000000E+1 4,000000E+1
AttenuationPosition = 1
...
I would like to extract the value of the exosure time and the attenuation value at attenuation posittion 1. The positions in this vector counts as pos 0,1,2, 3.
Any assistance is appreciated. Thanks!
2 Commenti
Risposte (1)
Constantino Carlos Reyes-Aldasoro
il 4 Apr 2023
You can solve this problem by finding strings in the text that you have. Let's save the example that you have shown in variable sampleText:
sampleText = ['Exposure Time = 135 s',...
'Area = -1,048161E-8 Vs',...
'Amplitude = 1,800100E-1 V',...
'Pulsewidth = 500,832700E-8 s',...
'Attenuation = 0,000000E+0 9,151000E+1 8,000000E+1 4,000000E+1',...
'AttenuationPosition = 1'];
Now we can find a particular string of text, for instance 'Exposure time =' and 's', which will give us where the values that you want start and finish
startString = 'Exposure Time = ';
startText1 = strfind(sampleText, startString);
endText1 = strfind(sampleText,'s');
disp(startText1)
disp(endText1)
The start is easy as there is just one case, but for the end there are many cases of 's'. Now, we know that the start is finding a string of length 16, and that will give us the clue of which 's' is the one we need.
lengthStart = numel(startString);
find(endText1>lengthStart,1)
We now know that the second case of 's' is the one we need, so now we know where is the text you want:
sampleText(lengthStart+1:endText1(2)-2)
Notice that we start 1 after the length of of the string, and finish 2 before the 's', that is 1 space + 1 for the 's' itself. So that is what you want, but remember this is a string, if you want the number, use str2num or str2double. Now you can do the same for any other thing you want to extract.
1 Commento
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!