How do I use inputdlg in a for loop to return a numerical array?

조회 수: 3 (최근 30일)
Gregory Hind
Gregory Hind 2020년 4월 10일
댓글: Gregory Hind 2020년 4월 11일
I need to make a program that asks the user how many floors there are and then input each floors dimensions in sequence and then store them in a numerical array for use later but I'm very new to this and struggling somewhat! The contentws of the for loop work fine outside the loop.
floors=inputdlg('enter number of floors: ');
for i=1:1:floors
dimensions={'Enter height','Enter width:','Enter length:'};
dlgtitle='Dimensions';
dims=[1 35];
answer=inputdlg(dimensions,dlgtitle,dims);
height(i)=str2num(answer{1});
width(i)=str2num(answer{2});
length(i)=str2num(answer{3});
end
  댓글 수: 2
Rik
Rik 2020년 4월 10일
I'm on mobile so I can't test your code, but I already see a potential source of issues: you're shadowing the internal function length() with a variable. You are also using str2num instead of the recommended str2double (which avoids an internal call to eval() with all the potential side effects).
Gregory Hind
Gregory Hind 2020년 4월 10일
편집: Rik 2020년 4월 11일
No problem, I converted the floors input to numerical using a str2double and now it works perfectly. What does shadowing the internal function mean? Thanks!
floors=inputdlg('enter number of floors: ');
floors=str2double(floors);
for i=1:1:floors
dimensions(i)={'Enter height','Enter width:','Enter length:'};
dlgtitle='Dimensions';
dims=[1 35];
answer=inputdlg(dimensions,dlgtitle,dims);
height(i)=str2num(answer{1});
width(i)=str2num(answer{2});
length(i)=str2num(answer{3});
end

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

채택된 답변

Rik
Rik 2020년 4월 11일
A few good practice tips:
  • avoid i and j as variable names to avoid confusion with sqrt(-1), avoid l (lowercase L) as well to avoid confusion with the numeral 1.
  • put all code that does not depend on your loop index outside of your loop so it is executed only once
  • don't use variable name like sum or length, as that will prevent you from using the internal functions with that name (this is called 'shadowing' of a function)
  • pay attention to the warnings mlint is giving you, try to resolve all warnings. If you are certain the warning doesn't apply in your case (which is rare), right-click the orange line and select the 'suppress warning on this line' option.
So here is your code with those tips applied:
n_floors=inputdlg('enter number of floors: ');
n_floors=str2double(n_floors);
dimensions={'Enter height','Enter width:','Enter length:'};
dlgtitle='Dimensions';
dims=[1 35];
height=zeros(1,n_floors);
width=zeros(1,n_floors);
len=zeros(1,n_floors);%you could consider depth as a variable name
for floor=1:n_floors
answer=inputdlg(dimensions,dlgtitle,dims);
height(floor)=str2double(answer{1});
width(floor)=str2double(answer{2});
len(floor)=str2double(answer{3});
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by