Code in the questions are tested in MATLAB 2023b. There is no guarantee of consistency in different version of MATLAB.