简体   繁体   中英

how to use inputdlg with for loop in matlab

have tried searching but can't find anything to help, maybe my problem is too simple! Anyway, I'm running a nested FOR loop, but the array I save my results to only keeps the last "run" of results. Can someone please help me to store/concatenate the results ?

clc 
clear
n = 2;
for aa = 1:n
aa = inputdlg({'Depth from','Depth to','Outer Diameter','Nominal Weight'},'1',[1 7;1 7;1 30;1 30]);
x = [str2num(aa{1}),str2num(aa{2}),str2num(aa{3}),str2num(aa{4})]
end

and the results x =

 1     2     3     4

x =

 5     6     7     8

I cannot use the first one, want to save all the results and save every iteration in a single variable

That is not the way to save the result into an array in a for loop; separate the looping variable and the array you are storing values in:

clc 
clear
n = 2;
x = zeros(n, 4);
for k = 1:n
    aa = inputdlg({'Depth from','Depth to','Outer Diameter','Nominal Weight'},'1',[1 7;1 7;1 30;1 30]);
    x(k, :) = [str2double(aa{1}),str2double(aa{2}),str2double(aa{3}),str2double(aa{4})];
end

Then if you display x you get something like:

x =

     1     2     3     4
     5     6     7     8

Assuming you entered 1 to 4 and then 5 to 8 in the dialog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM