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

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 日

0 投票

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

1 件のコメント

Gregory Hind
Gregory Hind 2020 年 4 月 11 日
That's excellent info, thank you very much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2020 年 4 月 10 日

コメント済み:

2020 年 4 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by