フィルターのクリア

How to concatenate table variables with underscores in their name

1 回表示 (過去 30 日間)
Peter P
Peter P 2019 年 7 月 23 日
編集済み: Adam Danz 2019 年 7 月 24 日
I have variables like B1_1_3(53x1) B2_1_3(53x1) until B56_1_3(53x1) stored in my table A. I want to concatenate them to receive a new vector(53x56). I tried:
for k in 1:56
a = [A.B{k}_1_3, A.B{k}_1_3];
end
However this does not work. Any suggestions? Thank you in advance!
  1 件のコメント
infinity
infinity 2019 年 7 月 23 日
What do you mean by "concatenate them to receive a new vector (53x56) "?
For example,
A = [1 2 3]
B = [1 2 3 4]
what is your output of a new matrix?

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2019 年 7 月 23 日
編集済み: Adam Danz 2019 年 7 月 23 日
No loop needed. Just list all of the table headers, pull out the ones that take the form "B#_1_3", and then create a matrix based on values from those columns.
% Create demo table
A = array2table(rand(6,4),'variableNames',{'B1_1_3','B2_1_3','B3_1_3','total'});
% List all headers
headers = A.Properties.VariableNames;
% Extract headers that have the form B#_1_3
goodHeaders = regexp(headers,'B\d+_1_3','Match');
goodHeaders = [goodHeaders{:}]';
% Create matrix of values from good header columns *see Guillaume's comment below
m = cell2mat(cellfun(@(x)A.(x)',goodHeaders,'UniformOutput',false))';
  2 件のコメント
Guillaume
Guillaume 2019 年 7 月 23 日
Why go through cellfun and cell2mat? m is simply:
m = A{:, goodHeaders}
Adam Danz
Adam Danz 2019 年 7 月 23 日
Another approach would be to remove the columns you don't want and keep the data in a table which has its advantages. That solution would look like this.
% Create demo table
A = array2table(rand(6,4),'variableNames',{'B1_1_3','B2_1_3','B3_1_3','total'});
% List all headers
headers = A.Properties.VariableNames;
% Extract headers that have the form B#_1_3
goodHeaderIdx = regexp(headers,'B\d+_1_3');
badHeaderIdx = cellfun(@isempty,goodHeaderIdx);
% Create matrix of values from good header columns
A2 = A;
A2(:,badHeaderIdx) = [];

サインインしてコメントする。

その他の回答 (1 件)

llueg
llueg 2019 年 7 月 23 日
編集済み: llueg 2019 年 7 月 24 日
You could use eval for this. Assuming your data in the table is numeric and you want a matrix as result:
a = zeros(53,56);
for i=1:56
eval(['a(:,i) = A.B' num2string(i) '_1_3'])
end
Edit: It has been pointed out that using eval is unnecessary here and bad in general. See the comments below for better solutions.
I just considered this to be closest to what OP was trying to do, but there are certainly better methods.
  3 件のコメント
Adam Danz
Adam Danz 2019 年 7 月 23 日
編集済み: Adam Danz 2019 年 7 月 24 日
There's no need for a loop and certainly no need to use eval(). This is bad advice (please look at the link provided by Guillaume above).
[update]
@llueg, thanks for clarifying your answer with your [edit].
Guillaume
Guillaume 2019 年 7 月 23 日
Gah! Why did you accept this answer. Again, eval is bad! extremely bad!. See the link in my comment above for all the ways that it is bad.
Do not use eval and certainly not for this. I've demonstrated a much more reliable method (and simpler, and faster, and etc... read the link).
Adam's method is even more reliable in that it doesn't assume that variables 1 to 56 do exist but check which ones exist.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by