Reading text file and searching for specific lines

Hello I am new to MATLAB, My question has 2 parts:- Part 1 I am trying to import .out / .txt file in matlab. I want to eliminate the top text and want to have only the UPPER COLOUM values which is named by "Eigenvalues computed with ARPACK in double precision".
Part 2 Then I wanted to find the position of Mode Counter 23, it should give me as 1, for 7 it should give me as 2 for 56 it should give me 3 for 26 it shoud give output as 4
Currently I am copying the UPPER COLOUM and then pasting it in excel. and then
dataset1=xlsread('40','Feuil1','B1:B21');
find(dataset1==23)
find(dataset1==7)
find(dataset1==56)
find(dataset1==26)
But this excel method seems to be good for few values and hectic for various values.

2 Comments

hello
can you share your file ?
Thanks Noe, but the file is too big to be shared here. If its okay I can share it via email ?
Here is the text file I copied for ease
DATA FOR THE CMA
Characteristic mode analysis with name: CharacteristicModes1
Number of characteristic modes requested: 20 ( 1.0521 % of eigenmode spectrum)
Memory requirement for calculation of the characteristic modes using CMA: 55.593 MByte
Number of orthogonal eigenmodes: 1
Eigenmode orthogonality could be improved by using a finer mesh,
switching to double precision accuracy or using a symmetrising
preconditioner for the matrix (only available for sequential runs)
Eigenvalues computed with ARPACK in double precision
Mode counter real part imaginary part
23 1.53204E-04 0.00000E+00
7 1.57794E-04 0.00000E+00
56 -2.04892E-04 0.00000E+00
26 2.98728E-04 0.00000E+00
54 3.28239E-04 0.00000E+00
50 3.62095E-04 0.00000E+00
48 -4.26659E-04 0.00000E+00
1 4.60090E-04 0.00000E+00
46 6.77002E-04 0.00000E+00
2 6.87158E-04 0.00000E+00
19 7.30916E-04 0.00000E+00
27 8.84706E-04 0.00000E+00
30 1.02893E-03 0.00000E+00
63 1.12071E-03 0.00000E+00
49 1.46597E-03 0.00000E+00
5 1.79261E-03 0.00000E+00
59 -2.28419E-03 0.00000E+00
44 2.35922E-03 0.00000E+00
61 -2.95337E-03 0.00000E+00
29 4.83191E-03 0.00000E+00
Modal excitation coefficients
Mode counter real part imaginary part
23 1.06403E-05 0.00000E+00
7 4.49871E-07 0.00000E+00
56 7.00694E-06 0.00000E+00
26 1.12852E-04 0.00000E+00
54 1.18304E-04 0.00000E+00
50 -9.83997E-06 0.00000E+00
48 6.57601E-04 0.00000E+00
1 1.76591E-05 0.00000E+00
46 3.66760E-05 0.00000E+00
2 -1.03195E-05 0.00000E+00
19 -5.68239E-06 0.00000E+00
27 -2.45799E-05 0.00000E+00
30 -2.87909E-05 0.00000E+00
63 9.11167E-06 0.00000E+00
49 2.38573E-04 0.00000E+00
5 1.28264E-03 0.00000E+00
59 -3.82674E-05 0.00000E+00
44 5.34647E-05 0.00000E+00
61 3.74705E-04 0.00000E+00
29 9.26106E-06 0.00000E+00
CPU time for characteristic mode analysis: 121.698 seconds
Run time for characteristic mode analysis: 121.699 seconds

Sign in to comment.

 Accepted Answer

hello again
so I tried with the .out file you shared
there are 10 blocks of data that correspond to you description (starting with "Eigenvalues computed with ARPACK in double precision")
so the code provided here will find these blocks an extract the 3 columns of data you are interested in :
then we will analyse each block of data and find / store the positions you wanted
for the data file provided , we have 10 blocks of data and the positions results are vectors of same length (10) :
fileDir = pwd; % define your working directory
filename = 'IGP_Small.out';
tic
D=readlines(fullfile(fileDir,filename)); % read as string array
% find start / stop lines of multiple blocks
idx1=find(contains(D,'computed with ARPACK in double precision')); % find the start lines index
idx2=find(contains(D,'Modal excitation coefficients')); % find the stop lines index
for ci = 1:numel(idx1)
D_extract = strtrim(D(idx1(ci)+2:idx2(ci)-2));
outdata = str2double(split(D_extract));
% store positions
tmp = find(outdata(:,1)==23);
if ~isempty(tmp)
position_Mode_Counter_23(ci) = tmp;
else
position_Mode_Counter_23(ci) = NaN;
end
%%%%%%
tmp = find(outdata(:,1)==7);
if ~isempty(tmp)
position_Mode_Counter_7(ci) = tmp;
else
position_Mode_Counter_7(ci) = NaN;
end
%%%%%%
tmp = find(outdata(:,1)==56);
if ~isempty(tmp)
position_Mode_Counter_56(ci) = tmp;
else
position_Mode_Counter_56(ci) = NaN;
end
%%%%%%
tmp = find(outdata(:,1)==26);
if ~isempty(tmp)
position_Mode_Counter_26(ci) = tmp;
else
position_Mode_Counter_26(ci) = NaN;
end
end
toc

9 Comments

Thanks Noe,
Your code works perfectly. But I want to know the position of various numbers aprt from 7,23,26,56. Say for 1,2,3,4... How did you actually extracted the coloums ?
hello again
first we search lines indexes that are considered as the start and end of the blocks
thiis is done by
% find start / stop lines of multiple blocks
idx1=find(contains(D,'computed with ARPACK in double precision')); % find the start lines index
idx2=find(contains(D,'Modal excitation coefficients')); % find the stop lines index
then we extract a string that represents (contains) all numerical values , and we remove the leading spaces with strtrim
D_extract = strtrim(D(idx1(ci)+2:idx2(ci)-2));
then we convert that string into numerical values
outdata = str2double(split(D_extract));
if you want to expand the code for other values than 7,23,26,56, you can follow the same logic
as here , simply change the target value (26 in the example below)
%%%%%%
tmp = find(outdata(:,1)==26);
if ~isempty(tmp)
position_Mode_Counter_26(ci) = tmp;
else
position_Mode_Counter_26(ci) = NaN;
end
but there is a better approach if you can tell all values you are looking at ; for the time being I was simply following your request for some specific values (7,23,26,56,)
Hie, What my main concern is that if I want to look for various numbers, as I can have different values. These numbers are not fixed.
Example i want to know for 5, I simply tried to change the target value as in picture attached but it gives an error.
If there is a better approach please let me know, I mean the code should be flexible enough to search from numbers 1 to 100.
well, already we can built a matrix containing side by side the 10 vectors of the first column from the data file.
so from left to right your have your "positions" numbers : for the 1st data block (left 1st column) to the 10th data block on the right (last column)
from there we can continue to refine the code but I have to understand how you want it
out =
1 7 7 23 23 23 19 50 26 23
2 1 1 7 7 7 23 23 30 7
3 2 23 1 1 19 7 7 7 56
4 23 19 19 19 50 46 46 23 26
5 5 2 26 26 1 50 1 50 54
6 14 26 2 50 46 1 19 2 50
7 26 5 50 2 26 26 26 1 48
8 27 27 30 30 2 54 2 46 1
9 19 30 5 5 30 2 54 19 46
10 28 44 44 54 54 30 30 54 2
11 29 28 27 46 5 27 27 27 19
12 30 29 54 44 27 5 56 56 27
13 31 45 29 27 44 49 5 49 30
14 32 46 46 48 57 44 48 5 63
15 33 42 28 29 48 57 49 48 49
16 34 47 48 28 29 48 44 44 5
17 35 39 42 49 49 29 29 29 59
18 36 48 39 57 28 28 59 59 44
19 37 49 49 39 59 59 61 63 61
20 38 50 56 59 39 56 57 61 29
fileDir = pwd; % define your working directory
filename = 'IGP_Small.out';
D=readlines(fullfile(fileDir,filename)); % read as string array
% find start / stop lines of multiple blocks
idx1=find(contains(D,'computed with ARPACK in double precision')); % find the start lines index
idx2=find(contains(D,'Modal excitation coefficients')); % find the stop lines index
out = [];
for ci = 1:numel(idx1)
D_extract = strtrim(D(idx1(ci)+2:idx2(ci)-2));
outdata = str2double(split(D_extract));
% store positions
out = [out outdata(:,1)];
end
Noe, Thanks again this code will be an added value to it. Your previous code seems to be very good. Only the thing i want in that code is: If I want to search for number 5 , how to do that ? Please refer to the picture attached. If i change the target value why i am not able to see the position ???
did you run the code after implementing the new section ?
it runs fine on my PC
position_Mode_Counter_5 = 5 5 7 9 9 11 12 13 14 16
fileDir = pwd; % define your working directory
filename = 'IGP_Small.out';
D=readlines(fullfile(fileDir,filename)); % read as string array
% find start / stop lines of multiple blocks
idx1=find(contains(D,'computed with ARPACK in double precision')); % find the start lines index
idx2=find(contains(D,'Modal excitation coefficients')); % find the stop lines index
out = [];
for ci = 1:numel(idx1)
D_extract = strtrim(D(idx1(ci)+2:idx2(ci)-2));
outdata = str2double(split(D_extract));
% % store positions
% out = [out outdata(:,1)];
%%%%%%
tmp = find(outdata(:,1)==5);
if ~isempty(tmp)
position_Mode_Counter_5(ci) = tmp;
else
position_Mode_Counter_5(ci) = NaN;
end
end
Thanks a lot it worked.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020b

Asked:

Zah
on 15 Dec 2022

Commented:

on 16 Dec 2022

Community Treasure Hunt

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

Start Hunting!