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

7 visualizaciones (últimos 30 días)
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 comentarios
Rik
Rik el 10 de Abr. de 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 el 10 de Abr. de 2020
Editada: Rik el 11 de Abr. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Rik
Rik el 11 de Abr. de 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

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by