# 文字列と数字の組み合わせをfor文でループさせる

38 views (last 30 days)
H.K on 2 Mar 2023
Commented: H.K on 2 Mar 2023
matlab初心者です。初歩中の初歩の質問で申し訳ございません。
for文で変数の繰り返しを行いたいのですが、うまく行きません。

ループさせるにはどうすれば良いでしょうか。ご回答の程宜しくお願い致します。

Hernia Baby on 2 Mar 2023
eval関数を使えばいけますがめちゃくちゃお勧めしません。

A = rand(36);
for jj = 1:width(A)
s = sprintf('x%i = A(:,%i);',jj,jj);
eval(s)
end

whos
Name Size Bytes Class Attributes A 36x36 10368 double cmdout 1x33 66 char jj 1x1 8 double s 1x14 28 char x1 36x1 288 double x10 36x1 288 double x11 36x1 288 double x12 36x1 288 double x13 36x1 288 double x14 36x1 288 double x15 36x1 288 double x16 36x1 288 double x17 36x1 288 double x18 36x1 288 double x19 36x1 288 double x2 36x1 288 double x20 36x1 288 double x21 36x1 288 double x22 36x1 288 double x23 36x1 288 double x24 36x1 288 double x25 36x1 288 double x26 36x1 288 double x27 36x1 288 double x28 36x1 288 double x29 36x1 288 double x3 36x1 288 double x30 36x1 288 double x31 36x1 288 double x32 36x1 288 double x33 36x1 288 double x34 36x1 288 double x35 36x1 288 double x36 36x1 288 double x4 36x1 288 double x5 36x1 288 double x6 36x1 288 double x7 36x1 288 double x8 36x1 288 double x9 36x1 288 double
------

for jj = 1:width(A)
C{jj} = A(:,jj);
end
C
C = 1×36 cell array
{36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double} {36×1 double}
2列目を取り出してみましょう
C{2}
ans = 36×1
0.1164 0.4850 0.7365 0.4477 0.5623 0.6490 0.1355 0.8852 0.0212 0.8059
H.K on 2 Mar 2023

### Categories

Find more on Logical 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!