Array Keeps creating 0's in my Loop

1 view (last 30 days)
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

Accepted Answer

David Fletcher
David Fletcher on 10 May 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

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!