Use loop to Summarize the Data using MATLAB

2 views (last 30 days)
clc
clear all
close all
X=randi(10,12,75);
Nz = 15;
TopTitle = compose('H%d_Features',1:Nz); % Prepare the top title
T = cell(1,Nz);
for k = 1:Nz
arr=X(:,5*(k-1)+1:5*(k-1)+5);
vnm =strcat({'Energy','variance','std','wl','entrophy'},num2str(k));
Ttemp = array2table(arr,'VariableNames',vnm);
T{k} = table(Ttemp,'VariableName',TopTitle(k));
end
Tcombine = cat(2,T {:});
Prasad=Tcombine(:,1:Nz);
%% Channel1 features %%
s11=Prasad.H1_Features(:,1);
s12=Prasad.H2_Features(:,1);
s13=Prasad.H3_Features(:,1);
s14=Prasad.H4_Features(:,1);
s15=Prasad.H5_Features(:,1);
This is My code,I would like to save Channel1 features into one variable using any loop technique.I don't want to use S11,S12,S13,S14 and S15 insted of that I need to use any loop technique and all the values of S11,S12,S13,S14 and S15 into one variable.Please help me with this

Accepted Answer

P.V Karthik
P.V Karthik on 11 Mar 2022
Can you try this:
out_arr=[];
out_mix=[];
for i=1:5
tab_out=eval(['Prasad.H',num2str(i),'_Features(:,1)']);
cell_out=table2cell(tab_out);
mat_out=cell2mat(cell_out);
out_arr=[out_arr;{['H_',num2str(i)],cell_out}];
out_mix=[out_mix;mat_out];
end
  2 Comments
Peter Perkins
Peter Perkins on 17 Mar 2022
Edited: Peter Perkins on 17 Mar 2022
A solution that involves calling cell2mat on the output of table2cell is almost certainly not the right solution. Indeed, calling table2cell in itself is suspect except in a very few cases. And certainly calling eval to do subscripting is a huge red flag.

Sign in to comment.

More Answers (1)

Peter Perkins
Peter Perkins on 11 Mar 2022
Not sure what you are trying to do. Maybe you are trying to get all the Energy data into one array? It's not clerar from your question. See if this use of inner2outer helps. Notice that I've also simplified your creation of Prasad:
X = randi(10,12,75);
Nz = 15;
%TopTitle = compose('H%d_Features',1:Nz); % Prepare the top title
TopTitle = compose("H%d_Features",1:Nz); % Prepare the top title
%T = cell(1,Nz);
Prasad = table;
for k = 1:Nz
arr = X(:,5*(k-1)+1:5*(k-1)+5);
%vnm = strcat({'Energy','variance','std','wl','entrophy'},num2str(k));
vnm = ["Energy" "variance" "std" "wl" "entrophy"];
%Ttemp = array2table(arr,'VariableNames',vnm);
%T{k} = table(Ttemp,'VariableName',TopTitle(k));
Prasad.(TopTitle(k)) = array2table(arr,'VariableNames',vnm);
end
Prasad
Prasad =
12×15 table
H1_Features H2_Features H3_Features H4_Features H5_Features H6_Features H7_Features H8_Features H9_Features H10_Features H11_Features H12_Features H13_Features H14_Features H15_Features
Energy variance std wl entrophy Energy variance std wl entrophy Energy variance std wl entrophy Energy variance std wl entrophy Energy variance std wl entrophy Energy variance std wl entrophy Energy variance std wl entrophy Energy variance std wl entrophy Energy variance std wl entrophy Energy variance std wl entrophy Energy variance std wl entrophy Energy variance std wl entrophy Energy variance std wl entrophy Energy variance std wl entrophy Energy variance std wl entrophy
___________________________________________ ___________________________________________ ___________________________________________ ___________________________________________ ___________________________________________ ___________________________________________ ___________________________________________ ___________________________________________ ___________________________________________ ___________________________________________ ___________________________________________ ___________________________________________ ___________________________________________ ___________________________________________ ___________________________________________
9 3 2 9 1 5 8 8 5 2 10 5 9 10 6 8 9 9 6 2 9 8 8 10 7 7 7 4 6 9 9 9 9 6 5 7 8 3 5 10 1 2 4 5 9 5 6 6 2 6 9 10 9 9 5 9 10 9 6 9 3 10 6 5 1 7 3 8 2 9 4 6 8 9 3
6 7 3 3 10 9 10 6 3 3 3 3 5 7 5 10 3 7 8 6 5 7 8 2 6 8 9 5 9 1 2 1 5 2 3 10 2 10 9 9 6 7 10 7 6 7 4 5 6 7 3 1 9 10 5 10 7 2 10 7 4 8 9 8 6 9 6 6 5 9 4 8 3 4 9
4 10 6 2 9 4 6 3 7 2 2 2 9 5 1 6 4 9 2 4 4 1 3 9 10 3 5 1 8 3 3 8 7 9 8 2 7 2 7 1 2 8 3 6 4 8 7 7 2 9 5 6 10 5 4 2 8 9 2 10 6 2 10 6 1 9 5 9 8 6 5 8 5 3 1
9 3 9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 6 2 2 2 3 2 7 3 7 3
3 2 10 10 10 10 6 4 2 4 10 7 6 1 10 7 7 4 10 2 5 5 6 5 9 9 5 8 2 7 8 9 10 3 3 1 7 7 1 5 6 1 6 8 9 2 6 1 8 9 9 7 2 4 5 10 10 6 8 8 3 8 10 10 5 2 2 6 3 2 9 9 5 7 2
1 6 5 9 9 3 4 1 6 5 6 4 6 1 6 2 8 7 4 7 9 2 6 1 5 9 5 8 10 10 1 5 8 6 3 1 10 4 1 10 7 4 5 9 9 7 1 1 5 6 3 10 3 1 1 10 7 8 10 10 10 5 10 1 4 4 1 1 6 9 8 2 2 10 7
>> Prasad2 = inner2outer(Prasad)
Prasad2 =
12×5 table
Energy variance std wl entrophy
__________ __________ __________ __________ __________
1×15 table 1×15 table 1×15 table 1×15 table 1×15 table
1×15 table 1×15 table 1×15 table 1×15 table 1×15 table
1×15 table 1×15 table 1×15 table 1×15 table 1×15 table
1×15 table 1×15 table 1×15 table 1×15 table 1×15 table
1×15 table 1×15 table 1×15 table 1×15 table 1×15 table
1×15 table 1×15 table 1×15 table 1×15 table 1×15 table
1×15 table 1×15 table 1×15 table 1×15 table 1×15 table
1×15 table 1×15 table 1×15 table 1×15 table 1×15 table
1×15 table 1×15 table 1×15 table 1×15 table 1×15 table
1×15 table 1×15 table 1×15 table 1×15 table 1×15 table
1×15 table 1×15 table 1×15 table 1×15 table 1×15 table
1×15 table 1×15 table 1×15 table 1×15 table 1×15 table
>> Prasad2.Energy
ans =
12×15 table
H1_Features H2_Features H3_Features H4_Features H5_Features H6_Features H7_Features H8_Features H9_Features H10_Features H11_Features H12_Features H13_Features H14_Features H15_Features
___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ____________ ____________ ____________ ____________ ____________ ____________
9 5 10 8 9 7 9 7 1 5 9 9 3 7 4
6 9 3 10 5 8 2 10 6 7 3 10 4 9 4
4 4 2 6 4 3 3 2 2 8 5 2 6 9 5
9 1 9 6 8 6 7 10 6 2 4 6 6 9 8
3 4 1 6 10 4 8 6 6 6 7 8 7 9 2
2 4 10 7 7 8 3 2 7 2 10 2 6 5 7
7 5 9 8 5 1 4 8 3 1 3 1 2 5 7
3 5 10 7 5 10 4 7 4 1 8 5 5 1 9
2 8 8 8 2 8 5 4 4 10 6 2 4 5 4
3 4 7 5 5 1 2 6 7 6 8 10 1 6 2
3 10 10 7 5 9 8 1 6 2 9 10 3 2 9
1 3 6 2 9 9 1 1 7 7 3 10 10 4 8

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!