How to have a changing variable within a user input?

조회 수: 9 (최근 30일)
Matt
Matt 2012년 8월 9일
I have the user input a number from 1-100. Here is the segment of my file that I'm working with.
Num = input('How many circles? ');
disp('Now enter the diameter of each circle one at a time when prompted')
for i=1:Num
D(:,i)= input('Diameter of circle ___ in mm: ')
end
I want the user to see "Diameter of circle 1 in mm? then "Diameter of circle 2 in mm? then "Diameter of circle 3 in mm? and so on until they have input the diameters for all of the circles.
I have tried to use num2str(i) but I haven't been able to get that to work. Later on I use num2str(i) within a disp text and that works fine. Any thoughts?

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 8월 9일
Num = input('How many circles? ');
disp('Now enter the diameter of each circle one at a time when prompted')
D = zeros(Num,1);
for i=1:Num
D(i)= input(['Diameter of circle ',num2str(i),' in mm: ']);
end

추가 답변 (1개)

Daniel Shub
Daniel Shub 2012년 8월 9일
Not sure how you tried to use num2str, but
D(:,i)= input(['Diameter of circle ', num2str(i), ' in mm: '])
works for me
  댓글 수: 1
Matt
Matt 2012년 8월 9일
I thought I tried using it that way. I even copied and pasted it from how I formated it within my disp text and it still didn't work. Guess I was just messing up somehow.
Thanks

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by