0

I have made a datafile from OpenFoam that extracts velocity at a certain location in time. I would like to extract two of these velocity and take there time average. For example I would like to extract the numbers: 0.0539764,0.0104665,0.00201741 and so on from probe 0. And extract the numbers: 0.690374, 0.711402, 0.699848 and so on from probe 1. How can this be done in Matlab?

I have done something similar before, but then the probes only consisted of 1 number (without the parentheses), now it consist of 3 numbers inscribed in a parentheses, I don't know what I am supposed to do.

Help is much appreciated.

Link to the whole file: https://drive.google.com/file/d/0B9CEsYCSSZUSdjFzYXVFc1RhM0k/view?usp=sharing enter image description here

Community
  • 1
  • 1
ursmooth
  • 311
  • 1
  • 4
  • 12
  • 2
    Learn to use regular expressions. In MATLAB use the [`regexp`](https://www.mathworks.com/help/matlab/ref/regexp.html) function. Being able to use regular expressions will serve you well no matter what type of programming work you do. – buzjwa Nov 01 '17 at 11:58

1 Answers1

1

This will create two matrices probe0 & probe1. You can index just the first column of each if that is all you are after.

id = fopen('testprobe.txt','r');
t = textscan(id,'%s','delimiter',sprintf('\n'));
fclose(id);

out = regexp(t{1,1}(6:end-3), '(?<=\()[^)]*(?=\))', 'match', 'all');

probe0 = zeros(size(out,1),3);
probe1 = zeros(size(out,1),3);

for i = 1:size(out,1)
    if ~isempty(out{i,:})
        probe0(i,:) = (str2double(split(out{i,1}{1,1})))';
        probe1(i,:) = (str2double(split(out{i,1}{1,2})))';
    else
        probe0(i,:) = [0,0,0];
        probe1(i,:) = [0,0,0];
    end

end
Brad Day
  • 400
  • 2
  • 9
  • Thank you for your answer, it is almost working, the matrix/vector dont seem to match up. Your code produces a 4361x1 vector, while [0.05:0.05:218.5]; is a 4370x1 vector, so there is 9 numbers missing. Do you have any ideas of what the problem could be? I adjusted the code to regexp(t{1,1}(5:end)........... – ursmooth Nov 04 '17 at 10:18