Filter löschen
Filter löschen

to extract information from a text file?

5 Ansichten (letzte 30 Tage)
Happy PhD
Happy PhD am 4 Apr. 2023
Bearbeitet: Happy PhD am 5 Apr. 2023
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 Kommentare
Stephen23
Stephen23 am 5 Apr. 2023
@Happy PhD: please upload a sample file by clicking the paperclip button.
Happy PhD
Happy PhD am 5 Apr. 2023
Bearbeitet: Happy PhD am 5 Apr. 2023
@Stephen23 Hi, I managed to find the file.
But uploading the data is difficult. It uploads as an cell array but I am not able to extract the exposure time or attenuation data, the latter one is an vector. Here I want the 1st postion in example (here would be position 2, if vector value 0 is 1 and value 91,5 is 2 etc.
'Exposure Time = 330 s'
'Attenuation = 0,000000E+0 9,151000E+1 8,000000E+1 4,000000E+1'
'AttenuationPosition = 1'
Uploaded an example of the text file.
My current code:
textfiles = dir('*.txt');
FileNames = strings(length(textfiles),1);
for k = 1:numel(textfiles)
N = textfiles(k).name;
disp(N)
if strfind(N,'_ZFocus_')
FileNames = N;
end % end if
end % end for
if isempty(FileNames)
disp('No text file found)')
return
end % end if
TextData = importdata(FileNames);
TData = TextData.textdata;
for i=1:length(TData)
sampleText= string(TData(i));
if strfind(sampleText,'_Exposure Time = ')
disp('found')
texpStr = sscanf(sampleText,'M%d');
end % end if
end % end for

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Constantino Carlos Reyes-Aldasoro
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)
1
disp(endText1)
5 21 43 72 96 171
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)
ans = 2
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)
ans = '135'
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 Kommentar
Happy PhD
Happy PhD am 5 Apr. 2023
Bearbeitet: Happy PhD am 5 Apr. 2023
Ok, but how do i upload the file that contains the data and extract for example the attenuation when it written:
'Attenuation = 0,000000E+0 9,151000E+1 8,000000E+1 4,000000E+1',...
'AttenuationPosition = 1'
I want to upload the attenuation as vector and get the value at pos 1 (i.e. 2 since it starts from pos 0, 1, 2,3.

Melden Sie sich an, um zu kommentieren.

Tags

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by