Array Keeps creating 0's in my Loop

1 Ansicht (letzte 30 Tage)
Gianni Davies
Gianni Davies am 10 Mai 2021
Kommentiert: Gianni Davies am 10 Mai 2021
The issue I am facing is my array (B) keeps reseting or losing the values that is being saved into it. I believe the issue is within the first three lines where I attempt to convert info(cell) into counter(double array)
B=[];
%I believe the issue lies here will I am getting the information for how
%long i want the loop to be
info=inputdlg('How many variables do you want to create a ratio for (2-5)');
convert1=cell2mat(info);
counter=str2double(convert1);
%I have done tests if I change counter to a number say '3' The B array
%will store information correctly
for p=1:1:counter
list = {'1','2','3','4','5'};
[indx,tf] = listdlg('PromptString',sprintf('Select Variable----->1=Resident , 2=Education , 3=Office , 4 =Toilet, 5=storage')...
,'ListSize',[500,250],'SelectionMode','single','ListString',list);
B(1,counter)=indx;
end

Akzeptierte Antwort

David Fletcher
David Fletcher am 10 Mai 2021
Your problem lies in the line
B(1,counter)=indx
counter is the max value of the loop and doesn't change - you should use the loop iterator p instead
B=[];
%I believe the issue lies here will I am getting the information for how
%long i want the loop to be
info=inputdlg('How many variables do you want to create a ratio for (2-5)');
convert1=cell2mat(info);
counter=str2double(convert1);
%I have done tests if I change counter to a number say '3' The B array
%will store information correctly
for p=1:1:counter
list = {'1','2','3','4','5'};
[indx,tf] = listdlg('PromptString',sprintf('Select Variable----->1=Resident , 2=Education , 3=Office , 4 =Toilet, 5=storage')...
,'ListSize',[500,250],'SelectionMode','single','ListString',list);
B(1,p)=indx;
end

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by