Array Keeps creating 0's in my Loop

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

David Fletcher
David Fletcher il 10 Mag 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

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by