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

3 views (last 30 days)
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 Comments
Rik
Rik on 10 Apr 2020
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 on 10 Apr 2020
Edited: Rik on 11 Apr 2020
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

Sign in to comment.

Accepted Answer

Rik
Rik on 11 Apr 2020
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

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!