Problem in showing sequence of static images for the desired amount of frames

Dear all, i kindly ask for your help. i have to show my participants a series of visual stimuli each lasting a certain amount of frames. each stimulus is coded as a number (reported in stimuli_matrix) and the amount of frames it should last is reported in stimframesVector. Rows of stimuli_matrix represents trials. My proble is that the stimulus is not shown for the specified amount of frames. As an alternative i tried to create a matrix containing: the number of frames a trial should last and the sequence of stimuli to present, i only obtained a flickering, inaccurate and wrong(timewise) stimulus presentation. Could you please help me in improving the code so that it shows the stimuli for the specified amount of frames without flickering?
here is the code
vbl = Screen(‘Flip’, window);
ifi = Screen(‘GetFlipInterval’, window);
Screen(‘Flip’,window);
for righe=1:size(stimuli_matrix,1)
for prov=1:size(stimuli_matrix,2)
waitframes=1;
stimduration = .5;
begduration = 1;
rndnumb=rand;
jitter= (rndnumb-0.5)*0.1;
Jittered_stim = stimduration + jitter;
Jittered_beg = begduration + jitter;

    % How long should the image stay up in frames
    
    Stim_Frames = round(Jittered_stim / ifi);
    
    % Duration in frames of trial start
    
    Beg_Frames = round(Jittered_beg / ifi);
    
    stimframesVector=ones(1,Stim_Frames);
    indice=stimuli_matrix(righe,prov)
    
    %stimframesVector1=stimframesVector(1)*1
    [keyIsDown,secs, keyCode] = KbCheck;
    if keyCode(escapeKey)
        sca;
    end
     tic;
    for frames=1:length(stimframesVector)
        if indice==0
            Screen('DrawLines', window, allCoords,lineWidthPix, white, [xCenter yCenter], 2);
            Screen('Flip', window, [], 1);
        elseif indice == 1
            Screen('DrawTextures', window, D1, [], allRects,0);
            Screen('Flip', window, [], 1);
        elseif indice == 2
            Screen('DrawTextures', window, C1, [], allRects,0);
            Screen('Flip', window, [], 1);
        elseif indice == 3
            Screen('DrawTextures', window, B1, [], allRects,0);
            Screen('Flip', window, [], 1);
        elseif indice == 4
            Screen('DrawTextures', window, A1, [], allRects,0);
            Screen('Flip', window, [], 1);
        elseif indice == 5
            Screen('DrawTextures', window, H1, [], allRects,0);
            Screen('Flip', window, [], 1);
        elseif indice == 6
            Screen('DrawTextures', window, G1, [], allRects,0);
            Screen('Flip', window, [], 1);
        elseif indice == 7
            Screen('DrawTextures', window, F1, [], allRects,0);
            Screen('Flip', window, [], 1);
        elseif  indice == 8
            Screen('DrawTextures', window, E1, [], allRects,0);
            Screen('Flip', window, [], 1);
        end
        
      
    end
 Screen('Flip', window, vbl+(waitframes-0.5)*ifi);
 stimvec2=stimframesVector %checking time
  toc; 
end

end