Concatenate variables with similar names

30 ビュー (過去 30 日間)
Ricardo Duarte
Ricardo Duarte 2021 年 6 月 8 日
編集済み: Ricardo Duarte 2021 年 6 月 8 日
Dear all
I need to concatenate several variables with similar name.
For example: T=cat(3,SPL_A,SPL_B,SPL_C,SPL_D,SPL_E).
This procedure is easy when you have just a few variables, as I show you in the previous line, however in my case I have hundreds of variables which makes almost impossible to do it by hand.
The variables have all the same starting name which I guess could help in the process, but Im not realizing how to do it.
Thanks in advance
  9 件のコメント
Ricardo Duarte
Ricardo Duarte 2021 年 6 月 8 日
Using a loop.
KSSV
KSSV 2021 年 6 月 8 日
So you are creating the variables. You could concatenate them while generating those variables in a loop.

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

採用された回答

Stephen23
Stephen23 2021 年 6 月 8 日
編集済み: Stephen23 2021 年 6 月 8 日
"Using a loop."
Then that is where you should fix the bad code design.
Meta-data is data, so store it in arrays, not in variable names.
Forcing meta-data (e.g. positions, frequencies) into variable names is one way that beginners force themselves into writing slow, inefficient, complex code that is liable to bugs and yet difficult to debug:
"this procedure is easy when you have just a few variables"
It is also very easy when you have an arbitrary number of arrays. The simple and efficient solution is to keep your data in just a cell array, then you can use comma-separated lists to concatenate those arrays:
For example:
N = number of arrays.
C = cell(1,N); % preallocate!
for k = 1:N
... your code
C{k} = whatever output array you want
end
M = cat(3,C{:}); % so simple.
  2 件のコメント
Ricardo Duarte
Ricardo Duarte 2021 年 6 月 8 日
Thank you, but this will only concatenate the names of the variables and I want to concatenate the content of each variable.
Ricardo Duarte
Ricardo Duarte 2021 年 6 月 8 日
編集済み: Ricardo Duarte 2021 年 6 月 8 日
It works, thank you

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

その他の回答 (1 件)

SALAH ALRABEEI
SALAH ALRABEEI 2021 年 6 月 8 日
編集済み: SALAH ALRABEEI 2021 年 6 月 8 日
@Ricardo Duarte Here is a trick, you will save all your workplace into one variable b; then loop over all of them to take whose variables who have the same size ( which are those you want to concatenate ). Assuming your variables that you need to concatenate are of size 20 x 30. However, if a any variable the same size as 20 x 30 will be concatenated
b = whos;
ConMat = [];
for i = 1:length(b)
if sum(b(i).size) == sum([20,30])
ConMat=[ConMat;eval(b(i).name)];
end
end
  6 件のコメント
SALAH ALRABEEI
SALAH ALRABEEI 2021 年 6 月 8 日
Thank you @Stephen Cobeldick, I have just check you invaluable complete Tech report in the importance of properly naming the varaibles. I didn't expect that until I read part of it.
Regarding to my comment, I was expecting a faster way to fix the issue of @Ricardo Duarte ( where there are several variables. Thank you though.
Stephen23
Stephen23 2021 年 6 月 8 日
編集済み: Stephen23 2021 年 6 月 8 日
"It would be great if there were a function that does so easily"
The comma-separated list syntax efficiently concatenates arbitrary numbers of arrays together.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by