ํ•„ํ„ฐ ์ง€์šฐ๊ธฐ
ํ•„ํ„ฐ ์ง€์šฐ๊ธฐ

How to create multiple strings

์กฐํšŒ ์ˆ˜: 14 (์ตœ๊ทผ 30์ผ)
Chao Zhang
Chao Zhang 2021๋…„ 5์›” 27์ผ
๋Œ“๊ธ€: Chao Zhang 2021๋…„ 5์›” 27์ผ
The following code, I use eval to create multiple similiar matrixs
num=input('Please enter the number of rock types:');
%Setup initial ore and waste matrix respectively
for i = 1 : num
eval(['M_ORE',num2str(i), ' =zeros(R1,C1)']);%multiple ore matrix
end
But I do not know how to create multiple strings
For example, the initial string is 'rn_mill_ton'
I want to input n=5, and the multiple strings such as 'r1_mill_ton', 'r2_mill_ton','r3_mill_ton','r4_mill_ton', and 'r5_mill_ton' are created.
Therefore, how to do that?
Thanks!
  ๋Œ“๊ธ€ ์ˆ˜: 1
Matt J
Matt J 2021๋…„ 5์›” 27์ผ
ํŽธ์ง‘: Matt J 2021๋…„ 5์›” 27์ผ

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.

์ฑ„ํƒ๋œ ๋‹ต๋ณ€

Jan
Jan 2021๋…„ 5์›” 27์ผ
Do not create variables dynamically using eval . See: TUTORIAL: How and why to avoid Eval
Hiding indices in the names of variables makes it cruel to access them later on. Use an array instead:
n = 5;
C = cell(1, n)
for k = 1:num
C{k} = sprintf('r%d_mill_ton', k);
end

์ถ”๊ฐ€ ๋‹ต๋ณ€ (1๊ฐœ)

Adam Danz
Adam Danz 2021๋…„ 5์›” 27์ผ
ํŽธ์ง‘: Adam Danz 2021๋…„ 5์›” 27์ผ
Don't use eval() at all. ๐Ÿ’€๐Ÿ’ฃ
Since all of the variables you're dynamically creating are the same size (R1xC1) you're better off creating a 3D array,
num=input('Please enter the number of rock types:');
M_ORE = zeros(R1,C1,num);
So to get the matrix for rock type number j,
M_ORE(:,:,j)
  ๋Œ“๊ธ€ ์ˆ˜: 1
Chao Zhang
Chao Zhang 2021๋…„ 5์›” 27์ผ
Thank you๏ผ

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.

์นดํ…Œ๊ณ ๋ฆฌ

Help Center ๋ฐ File Exchange์—์„œ Characters and Strings์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

ํƒœ๊ทธ

Community Treasure Hunt

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

Start Hunting!

Translated by