Clear Filters
Clear Filters

How do I choose specific data from a .txt file and then find the average, max and min values

7 views (last 30 days)
Hi, I'm fairly new to using matlab and I would like to know how to make a generic code so that I can extract the figures in bold in order to find the average, minimum and maximum values even when their values change. Any help with this wouldbe appreciated thanks.
iQ-Analyzer V6.2.2.1
FileName: U:\test.txt
FileDate: 04-Feb-2020 14:18:05
ChartFile: TE281
FileName dark: -
Make: - Firmware: noExif
Model: - Serial: Lens:
Aperture: 0.0 Aperture_eff: 0.0
Exp.Time: 0.000000
ISO: 0
Exp.Bias: -
FocalLength: 0.0
Measurement type: C
X [Pixel]: 1280 Y [Pixel]: 800 X [mm]: Y [mm]: PixelCount [MP]: 1.0 PixelPitch [�m]:
Illumination [Lux]: 1000
Notes:
F[%]
Pos. Avg. field Average F[%] Q1 field Q1 F[%] Q2 field Q2 F[%] Q3 field Q3 F[%] Q4 field Q4 F[%]
Center 0.00467498 11.9126 0.00467498 11.9126 0.00467498 11.9126 0.00467498 11.9126 0.00467498 11.9126
H1 0.178026 9.93692 0.176921 9.88428 0.174292 9.6432 0.179058 9.96143 0.181832 10.2588
H2 0.358703 8.7754 0.359936 8.77699 0.354776 8.6757 0.357503 8.78022 0.362599 8.86868
H3 0.538969 8.58128 0.543139 8.74501 0.535175 8.81967 0.533882 8.37896 0.543682 8.38149
H4 0.714701 9.32563 0.724523 9.71568 0.710426 9.47228 0.704348 8.96331 0.719507 9.15123

Accepted Answer

Marcelo
Marcelo on 7 Feb 2020
Try this:
fid = fopen('test.txt');
is_start_point = false;
values = {};
while 1
current_line = fgetl(fid);
if ~ischar(current_line), break, end
if strcmp(strtrim(current_line),'F[%]')
is_start_point = true;
continue
end
if is_start_point
broken_line = strsplit(current_line, '\t', 'CollapseDelimiters', true);
broken_line(cellfun('isempty',broken_line)) = [];
values = [values;broken_line];
end
end
fclose(fid);
title = values(1,:);
numbers = values(2:end,2:end);
numbers = cellfun(@str2double,numbers);
max_number = max(numbers)
min_number = min(numbers)
mean_number = mean(numbers)
Maybe, depend on your Matlab version, some commands won't work. In this case, just search for another one similar in your version.
  3 Comments
Marcelo
Marcelo on 10 Feb 2020
Hi, Sam.
Maybe the test.txt file I'm using is not exactly equal yours. Could you attach an example file to better work on this problem?
By the way, which Matlab version are you using?

Sign in to comment.

More Answers (1)

Marcelo
Marcelo on 7 Feb 2020
Sam,
Is your text file exactly like this one you pasted in your question? I mean, the eleventh element is actually in a new line or this just happened when you pasted the values here?
  1 Comment
Sam Bailey
Sam Bailey on 7 Feb 2020
Edited: Sam Bailey on 7 Feb 2020
Hi Marcelo, this just happened when I pasted the values here, this is what the text file looks like;
I need all the information in the 5th column and everything but the first row of numbers in the 7th, 9th and 11th columns. Then I would like to find the average, max and min values.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!