Need guidance: Extracting variable and values from log file

3 Ansichten (letzte 30 Tage)
Aamir
Aamir am 16 Mai 2023
Kommentiert: Aamir am 17 Mai 2023
Hi,
I have a log file with three different algorithms time elapsed data along with number of users for each run. I have tried different methods but I am not able to exactly copy the data in a different variables. Kindly guide me where I am doing mistake?
Also guide me how I can automatically extract variables by matching the strings? I am using this question under this weblink as guideline.
fclose('all')
ans = 0
clear all
%% ========================
fid=fopen('Simulation_Results.txt');
% This file contains variable names and variable values
values = textscan(fid, '%s', 'delimiter','\t','MultipleDelimsAsOne',1) ;
fclose(fid)
ans = 0
%% ========================
all_variable = values{1,1};
len= length(values{1,1});
for i = 2:2:len
numUser{i}= all_variable{i, 1};
%numUsr(i) = cell2mat(numUser(i));%% giving error
end
%% ========================
for j = 1:6:len
Time_Alg1{j}= all_variable{j, 1};
%Time_A1(i) = cell2mat(Time_Alg1(i));%% giving error
end
for k= 3:6:len
Time_Alg2{k}= all_variable{k, 1};
%Time_A2(i) = cell2mat(Time_Alg2(i)); %% giving error
end
for l = 5:6:len
Time_Alg3{l}= all_variable{l, 1};
%Time_A3(i) = cell2mat(Time_Alg3(i));%% giving error
end
%% ========================
%%% ================================ Log File Snippet =================
Time Elapsed Algorithm 1: 0.226373
Num of Users: 9
Time Elapsed Algorithm 2: 0.301075
Num of Users: 9
Time Elapsed Algorithm 3: 0.302607
Num of Users: 9
Time Elapsed Algorithm 1: 0.543193
Num of Users: 18
Time Elapsed Algorithm 2: 0.571813
Num of Users: 18
Time Elapsed Algorithm 3: 0.813169
Num of Users: 18
Time Elapsed Algorithm 1: 1.07906
Num of Users: 27
Time Elapsed Algorithm 2: 1.05138
Num of Users: 27
Time Elapsed Algorithm 3: 0.999459
Num of Users: 27

Akzeptierte Antwort

Stephen23
Stephen23 am 16 Mai 2023
Bearbeitet: Stephen23 am 16 Mai 2023
str = fileread('Simulation_Results.txt')
str =
'Time Elapsed Algorithm 1: 0.226373 Num of Users: 9 Time Elapsed Algorithm 2: 0.301075 Num of Users: 9 Time Elapsed Algorithm 3: 0.302607 Num of Users: 9 Time Elapsed Algorithm 1: 0.543193 Num of Users: 18 Time Elapsed Algorithm 2: 0.571813 Num of Users: 18 Time Elapsed Algorithm 3: 0.813169 Num of Users: 18 Time Elapsed Algorithm 1: 1.07906 Num of Users: 27 Time Elapsed Algorithm 2: 1.05138 Num of Users: 27 Time Elapsed Algorithm 3: 0.999459 Num of Users: 27 Time Elapsed Algorithm 1: 1.71224 Num of Users: 36 Time Elapsed Algorithm 2: 1.37632 Num of Users: 36 Time Elapsed Algorithm 3: 1.42004 Num of Users: 36 Time Elapsed Algorithm 1: 1.80948 Num of Users: 45 Time Elapsed Algorithm 2: 1.37436 Num of Users: 45 Time Elapsed Algorithm 3: 1.65804 Num of Users: 45 Time Elapsed Algorithm 1: 1.88339 Num of Users: 54 Time Elapsed Algorithm 2: 1.68589 Num of Users: 54 Time Elapsed Algorithm 3: 1.67572 Num of Users: 54 Time Elapsed Algorithm 1: 2.75575 Num of Users: 63 Time Elapsed Algorithm 2: 2.58013 Num of Users: 63 Time Elapsed Algorithm 3: 2.33181 Num of Users: 63 Time Elapsed Algorithm 1: 4.09326 Num of Users: 72 Time Elapsed Algorithm 2: 3.20119 Num of Users: 72 Time Elapsed Algorithm 3: 3.1917 Num of Users: 72 Time Elapsed Algorithm 1: 6.97589 Num of Users: 81 Time Elapsed Algorithm 2: 4.13146 Num of Users: 81 Time Elapsed Algorithm 3: 4.14442 Num of Users: 81 Time Elapsed Algorithm 1: 11.3555 Num of Users: 90 Time Elapsed Algorithm 2: 5.18295 Num of Users: 90 Time Elapsed Algorithm 3: 5.1702 Num of Users: 90 Time Elapsed Algorithm 1: 20.0274 Num of Users: 99 Time Elapsed Algorithm 2: 6.34679 Num of Users: 99 Time Elapsed Algorithm 3: 6.32009 Num of Users: 99 Time Elapsed Algorithm 1: 31.1671 Num of Users: 108 Time Elapsed Algorithm 2: 7.55464 Num of Users: 108 Time Elapsed Algorithm 3: 7.56211 Num of Users: 108 Time Elapsed Algorithm 1: 47.7765 Num of Users: 117 Time Elapsed Algorithm 2: 8.93815 Num of Users: 117 Time Elapsed Algorithm 3: 8.88728 Num of Users: 117 Time Elapsed Algorithm 1: 74.1714 Num of Users: 126 Time Elapsed Algorithm 2: 10.3712 Num of Users: 126 Time Elapsed Algorithm 3: 10.3819 Num of Users: 126 Time Elapsed Algorithm 1: 112.762 Num of Users: 135 Time Elapsed Algorithm 2: 11.9439 Num of Users: 135 Time Elapsed Algorithm 3: 11.9405 Num of Users: 135 Time Elapsed Algorithm 1: 171.703 Num of Users: 144 Time Elapsed Algorithm 2: 13.6046 Num of Users: 144 Time Elapsed Algorithm 3: 13.6214 Num of Users: 144 Time Elapsed Algorithm 1: 227.588 Num of Users: 153 Time Elapsed Algorithm 2: 15.4473 Num of Users: 153 Time Elapsed Algorithm 3: 15.4624 Num of Users: 153 Time Elapsed Algorithm 1: 378.988 Num of Users: 162 Time Elapsed Algorithm 2: 17.2862 Num of Users: 162 Time Elapsed Algorithm 3: 17.332 Num of Users: 162 Time Elapsed Algorithm 1: 510.075 Num of Users: 171 Time Elapsed Algorithm 2: 19.3588 Num of Users: 171 Time Elapsed Algorithm 3: 19.2924 Num of Users: 171 Time Elapsed Algorithm 1: 712.09 Num of Users: 180 Time Elapsed Algorithm 2: 21.4451 Num of Users: 180 Time Elapsed Algorithm 3: 21.4941 Num of Users: 180 Time Elapsed Algorithm 1: 1259.03 Num of Users: 189 Time Elapsed Algorithm 2: 23.7443 Num of Users: 189 Time Elapsed Algorithm 3: 23.5701 Num of Users: 189 Time Elapsed Algorithm 1: 1374.62 Num of Users: 198 Time Elapsed Algorithm 2: 26.0626 Num of Users: 198 Time Elapsed Algorithm 3: 25.9014 Num of Users: 198 '
tkn = regexp(str,'(\d+):\s*(\d\S+)\s*\n[^\n]+:\s*(\d+)','tokens');
mat = str2double(vertcat(tkn{:}))
mat = 66×3
1.0000 0.2264 9.0000 2.0000 0.3011 9.0000 3.0000 0.3026 9.0000 1.0000 0.5432 18.0000 2.0000 0.5718 18.0000 3.0000 0.8132 18.0000 1.0000 1.0791 27.0000 2.0000 1.0514 27.0000 3.0000 0.9995 27.0000 1.0000 1.7122 36.0000
  5 Kommentare
Stephen23
Stephen23 am 16 Mai 2023
"Furthermore if I want to extract full string and want to save that strings as variable name ... I have 45 variables in 3 different log file so I want to extract all those variables from log file using strings extraction technique and update their value from the log file to use for plotting."
Do not access 45 varaible names dynamically like that:
If you really want the text as well, then just use a table, e.g.:
tbl = readtable('Simulation_Results.txt','Delimiter',':');
tbl = 132×2 table
Var1 Var2 ____________________________ _______ {'Time Elapsed Algorithm 1'} 0.22637 {'Num of Users' } 9 {'Time Elapsed Algorithm 2'} 0.30107 {'Num of Users' } 9 {'Time Elapsed Algorithm 3'} 0.30261 {'Num of Users' } 9 {'Time Elapsed Algorithm 1'} 0.54319 {'Num of Users' } 18 {'Time Elapsed Algorithm 2'} 0.57181 {'Num of Users' } 18 {'Time Elapsed Algorithm 3'} 0.81317 {'Num of Users' } 18 {'Time Elapsed Algorithm 1'} 1.0791 {'Num of Users' } 27 {'Time Elapsed Algorithm 2'} 1.0514 {'Num of Users' } 27
And then use e.g.:
Keeping data together makes it much easier to work with.
Aamir
Aamir am 17 Mai 2023
@Stephen23 Thanks for giving good suggestion and helping me out.
I need your help regarding getting the Min. Int values each time for different indicies. I have attached a resultarray.mat for your reference.
I am checking first time a minimum value and then getting the indices of that minimum value. Now I want to discard that indices or the whole entry for the time being so that I don't get that minimum value again and check for other indicies. Since i have three slots and I want unique indicies in each slot and in each slot there can b n number of users. But when I tried commented code it gives error that Index in position 2 exceeds array bounds.
Kindly guide me how to find Min. Int and Indices.
num_users = 9:9:999
gain_len = reshape(num_users, [], 3);
for ind = 1: gain_len % Gain length depends on the number of Users gain_len =
[minIntAlg1(aa),min_int_PAind(aa)] = min(Tot_Int_PAcomb1(:,1)); %
results_array(aa,:) = Tot_Int_PAcomb1(min_int_PAind(aa),:);
Bst_P_CombA1_values(aa,:) = results_array(aa,2:4);
Int_minInt_Alg1(aa) = results_array(aa,1);
P_minInt_Alg1(aa) = sum(results_array(aa,2:4));
R_minInt_Alg1(aa) = sum(results_array(aa,9:11));
E_minInt_Alg1(aa) = sum(results_array(aa,12:14));
result(aa).resultarry = results_array(aa,:);
result(aa).Tot_Intr_PAcomb1 = Tot_Intr_PAcomb1;
minintPA_Out(aa) = minIntAlg1(aa);
P12_indicesA2(aa,:) = Tot_Int_PAcomb1(min_int_PAind(aa),2:end);
Bst_P_CombA1_values(aa,:) = P12_indicesA2(aa,5:7);
% The commented code i tried but it gives error as the temp folder get
% empty after 6-7 repitition as it match the indicies in
% Tot_Int_comb_temp(Tot_Int_comb_temp(:,5)==Bst_P_CombA1_values(ind, 1),:)=[];
% Tot_Int_comb_temp(Tot_Int_comb_temp(:,6)==Bst_P_CombA1_values(ind, 2),:)=[];
% Tot_Int_comb_temp(Tot_Int_comb_temp(:,7)==Bst_P_CombA1_values(ind, 3),:)=[];
% Tot_Int_PAcomb1 = Tot_Int_comb_temp;
end
Bst_P_CombA1_values_run1 =
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
Bst_P_CombA1_values_run2 =
5 4 5
4 5 4
3 1 1
2 2 2
1 3 3
Bst_P_CombA1_values_run2 =
5 5 5
4 4 4
3 1 3
2 2 2
1 3 1
% Error Message
% Index in position 2 exceeds array bounds. Index must not exceed 4.
%Error in Sim_Int_Rate_EE_PA_Comb_Code_11May2023 (line 540)
% Tot_Int_comb_temp(Tot_Int_comb_temp(:,5)==Bst_P_CombA1_values(ind, 1),:)=[];

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by