フィルターのクリア

How do I designate some variables in the same time that are starting with specific characters?

2 ビュー (過去 30 日間)
Hello,
It's a quite simple question.
For example, I have variables named n1, n2, and n3. They have similar names starting with n.
And, those are all cell structure.
I want to exchange this variables into double using cell2mat function.
I want to designate those in the same time and change into double.
I tried
n*=cell2mat(n*)
but, It didn't work.
How can I change this?
Sorry for my bad English. If you have any question regarding this, feel free to ask.
Thanks,
Hyojae.
  1 件のコメント
Stephen23
Stephen23 2023 年 3 月 13 日
編集済み: Stephen23 2023 年 3 月 13 日
"For example, I have variables named n1, n2, and n3. They have similar names starting with n."
Numbering variable names like that is usually a bad data design...
because accessing numbered variable names (like you have) is slow, complex, and inefficient:
You should use indexing. Indexing is simple (just as Matthieu showed below) and very efficient.

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

採用された回答

Matthieu
Matthieu 2023 年 3 月 3 日
Hi,
n = cell(1,3) ; % Replacing your n1, n2, n3 notation with n{1}, n{2}, n{3}
for i = 1:3
n{i} = {1,2,3} ; % Define n{i} cells as you want
end
for j = 1:3
n{j} = cell2mat(n{j}) ; % Converting cells to double
end
n
n = 1×3 cell array
{[1 2 3]} {[1 2 3]} {[1 2 3]}
Is this what you wanted ?
  2 件のコメント
HyoJae Lee
HyoJae Lee 2023 年 3 月 6 日
Sorry for the late reply!
Appreciate, Matthieu!!
Peter Perkins
Peter Perkins 2023 年 3 月 13 日
If n1, n2, and n3 end up being column vectors all with the same length, you might find it useful to put them into a table rather than a cell array. For example, something like
n1 = {1;2;3}; n2 = {4;5;6}; n3 = {7;8;9};
t = table(n1,n2,n3)
t = 3×3 table
n1 n2 n3 _____ _____ _____ {[1]} {[4]} {[7]} {[2]} {[5]} {[8]} {[3]} {[6]} {[9]}
t.n1
ans = 3×1 cell array
{[1]} {[2]} {[3]}
t = varfun(@cell2mat,t)
t = 3×3 table
cell2mat_n1 cell2mat_n2 cell2mat_n3 ___________ ___________ ___________ 1 4 7 2 5 8 3 6 9
t.cell2mat_n1
ans = 3×1
1 2 3

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAcoustics, Noise and Vibration についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by