My problem is as follows:
I have 3 different listings environments namely List of Matlab Outputs, List of Matlab Code and List of Pseudocodes. However all listings are under List of Matlab Outputs while the List of Codes and Pseudocodes are empty which is not supposed to be the case. I want all Matlab outputs (e.g Matlab Output 1.1,...) to be under List of Matlab Outputs, all Matlab codes (e.g Matlab Code 1.1,...) to be under List of Matlab Codes and all Pseudocodes (e.g Pseudocode 1.1,...) to be under List of Pseudocodes as shown in the picture below: 
I am looking for a solution that solves the problem in presence of all the packages I loaded. I suspect there are some lines of code that are causing this problem considering the fact that if some lines of code are deleted, I get the desired output. I have attached a picture showing the problem I am facing. I am writing a book so please I want the loaded packages not to be deleted when providing the solution. Below is the picture showing the problem and my MWE respectively:

\documentclass[11pt,openany,twoside]{book}
\raggedbottom
\let\cleardoublepage=\clearpage
\usepackage[left=2.5cm, right=2.5cm, top=3cm, bottom=3cm,a4paper]
{geometry}
\usepackage{parskip}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{array}
\newcolumntype{I}{!{\vrule width 1pt}}
\newlength\savedwidth
\newcommand\whline{\noalign{\global\savedwidth\arrayrulewidth
\global\arrayrulewidth 1pt}%
\hline
\noalign{\global\arrayrulewidth\savedwidth}}
\usepackage{xcolor, graphicx}
\newcommand\HRule{\noindent\rule{\linewidth}{1.5pt}}
\usepackage[dotinlabels]{titletoc}
\usepackage{background}
\backgroundsetup{contents={}}
\usepackage{helvet}
\usepackage{color}
\usepackage{fancyhdr}
\usepackage{hhline}
\pagestyle{fancy}
\usepackage{booktabs}
\usepackage[noindentafter,calcwidth]{titlesec}
\definecolor{mintbg}{rgb}{.63,.79,.95}
\usepackage{longtable}
\usepackage{multirow}
\usepackage{colortbl}
\newcommand*\myheaderfooterfont{\normalfont}
\usepackage[nottoc]{tocbibind}
\usepackage{verbatim}
\usepackage[ragged]{sidecap}
\usepackage{caption}
\usepackage{tocloft}
\renewcommand{\cfttoctitlefont}{\hfill\normalfont}
\renewcommand{\cftloftitlefont}{\hfill\normalfont}
\renewcommand{\cftlottitlefont}{\hfill\normalfont}
%\renewcommand{\cftafterloftitle}{\hfill}
\setlength\cftbeforetoctitleskip{-2cm}
\setlength\cftbeforeloftitleskip{-2cm}
\setlength\cftbeforelottitleskip{-2cm}
\renewcommand{\cftchapfont}{\normalfont}
\renewcommand{\cftsecfont}{\normalfont}
\renewcommand{\cftsubsecfont}{\normalfont}
\usepackage{regexpatch}
\usepackage{listings}
\usepackage[numbered, framed]{matlab-prettifier}
\let\ph\mlplaceholder % shorter macro
\lstMakeShortInline"
\makeatletter
% --------------------------------------- C++
\newcommand{\lstlistmatlaboutputname}{List of Matlab Output}
\lst@UserCommand\lstlistofmatlaboutput{\bgroup
\let\contentsname\lstlistmatlaboutputname
\let\lst@temp@starttoc \def@starttoc##1{\lst@temp{loc}}%
\tableofcontents \egroup}
\lstnewenvironment{matlaboutput}[1][]{%
\renewcommand{\lstlistingname}{Matlab Output}%
\xpatchcmd{\lst@MakeCaption}{lol}{loc}{}{}%
\lstset{language=C++,#1}}
{}
% --------------------------------------- R
\newcommand{\lstlistmatlabcodename}{List of Matlab Code}
\lst@UserCommand\lstlistofmatlabcode{\bgroup
\let\contentsname\lstlistmatlabcodename
\let\lst@temp@starttoc \def@starttoc##1{\lst@temp{lor}}%
\tableofcontents \egroup}
\lstnewenvironment{matlabcode}[1][]{%
\renewcommand{\lstlistingname}{Matlab Code}%
\xpatchcmd{\lst@MakeCaption}{lol}{lor}{}{}%
\lstset{language=R,#1}}
{}
% --------------------------------------- Pseudocode
\newcommand{\lstlistpseudocodename}{List of Pseudocode}
\lst@UserCommand\lstlistofpseudocode{\bgroup
\let\contentsname\lstlistpseudocodename
\let\lst@temp@starttoc \def@starttoc##1{\lst@temp{lop}}%
\tableofcontents \egroup}
\lstnewenvironment{pseudocode}[1][]{%
\renewcommand{\lstlistingname}{Pseudocode}%
\xpatchcmd*{\lst@MakeCaption}{lol}{lop}{}{}%
\lstset{basicstyle=\ttfamily,#1}}
{}
\makeatother
\renewcommand{\lstlistofmatlaboutput}{\begingroup
\tocfile{\lstlistmatlaboutputname}{lol}
\endgroup}
\renewcommand{\lstlistofmatlabcode}{\begingroup
\tocfile{\lstlistmatlabcodename}{lol}
\endgroup}
\renewcommand{\lstlistofpseudocode}{\begingroup
\tocfile{\lstlistpseudocodename}{lol}
\endgroup}
\makeatletter
\pretocmd{\chapter}{\addtocontents{toc}{\protect\addvspace{-4\p@}}}
{}{}
\makeatother
\renewcommand\cftchapafterpnum{\vskip-4pt}
\usepackage{hyperref}
\hypersetup{
colorlinks=true,
linkcolor=blue
}
\usepackage{filecontents}
\begin{filecontents}{person.m}
classdef person
properties %(here, properties is a keyword)
mass=80;
height=1.80;
end
methods
function BMI = getBMI(height,weight)
BMI = person.mass/person.mass^2;
end
end
end
\end{filecontents}
\begin{filecontents}{sample.m}
%% Code sections are highlighted.
% System command are supported...
!gzip sample.m
% ... as is line continuation.
A = [1, 2, 3,... % (mimicking the ouput is good)
4, 5, 6]
fid = fopen('testFile.text', 'w')
for i=1:10
fprintf(fid,'%6.2f \n', i);
end
x=1; %% this is just a comment, though
% Context-sensitive keywords get highlighted correctly...
p = properties(mydate); %(here, properties is a function)
x = linspace(0,1,101);
y = x(end:-1:1)
% ... even in nonsensical code.
]end()()(((end end)end ))))end (function end
%{
block comments are supported
%} even
runaway block comments
are
\end{filecontents}
\begin{document}
\lstlistofmatlaboutput
\lstlistofmatlabcode
\lstlistofpseudocode
\chapter{Listings}
\begin{matlaboutput}[caption = {Some class definition}]
classdef person
properties %(here, properties is a keyword)
mass=80;
height=1.80;
end
methods
function BMI = getBMI(height,weight)
BMI = person.mass/person.mass^2;
end
end
end
\end{matlaboutput}
\begin{matlabcode}[caption = {For educational purposes}]
% example of while loop using placeholders
while "\ph{condition}"
if "\ph{something-bad-happens}"
break
else
% do something useful
end
% do more things
end
\end{matlabcode}
\begin{matlabcode}[caption = {Sample code from Matlab}]
%% Code sections are highlighted.
% System command are supported...
!gzip sample.m
% ... as is line continuation.
A = [1, 2, 3,... % (mimicking the ouput is good)
4, 5, 6]
fid = fopen('testFile.text', 'w')
for i=1:10
fprintf(fid,'%6.2f \n', i);
end
x=1; %% this is just a comment, though
% Context-sensitive keywords get highlighted correctly...
p = properties(mydate); %(here, properties is a function)
x = linspace(0,1,101);
y = x(end:-1:1)
% ... even in nonsensical code.
]end()()(((end end)end ))))end (function end
%{
block comments are supported
%} even
runaway block comments
are
\end{matlabcode}
\begin{pseudocode}[caption={Hello world}]
print "Hello world"
\end{pseudocode}
\end{document}





