MATLAB: Showing x and y Labels in a Correlation Matrix Plot

corrplot

Would you please help me to show m_{NPV} with their subscripts as below:
VariableNames={'\sigma_{1}','\sigma_{2}','\sigma_{3}','\tau_{1}','\tau_{4}','\tau_{5}','m_{NPV}'};
So, I have this code which works but I do not know how to fix m_{NPV}
% your example code
Fields = [1, 4, 5];
Fields_time = Fields +16;
MT_All = rand(100,26);
% MT_All = rand(100,9);
VariableNames={'sigma','sigma','sigma','tau','tau','tau','mNPV'}; % changed to ensure valid syntax
Mat_All_1_4_5 = [MT_All(:,Fields), MT_All(:,Fields_time), MT_All(:,end-1)];
figure
corrplot(Mat_All_1_4_5, 'varNames', VariableNames);
% get current figure handle
fh = gcf;
% find x and y label strings that are not empty within subplots
yLabelN = find(cell2mat(arrayfun(@(dIn)~isempty(dIn.YLabel.String),fh.Children,'UniformOutput',false)));
xLabelN = find(cell2mat(arrayfun(@(dIn)~isempty(dIn.XLabel.String),fh.Children,'UniformOutput',false)));
% rename y labels
% indSig = 0;
% for ik = 1:length(yLabelN)
%%

for ik = 1:length(Fields)
if strfind(fh.Children(yLabelN(ik)).YLabel.String,'sigma')
% indSig = indSig + 1;

fh.Children(yLabelN(ik)).YLabel.String = strrep(fh.Children(yLabelN(ik)).YLabel.String,'sigma',sprintf('\\sigma_{%d}',Fields(ik)));
end
if strfind(fh.Children(yLabelN(ik+length(Fields))).YLabel.String,'tau')
% indSig = indSig + 1;
fh.Children(yLabelN(ik+length(Fields))).YLabel.String = strrep(fh.Children(yLabelN(ik+length(Fields))).YLabel.String,'tau',sprintf('\\tau_{%d}',Fields(ik)));
end
% if strfind(fh.Children(yLabelN(ik+length(Fields))).YLabel.String,'m')
% % indSig = indSig + 1;
% fh.Children(yLabelN(ik+length(Fields))).YLabel.String = strrep(fh.Children(yLabelN(ik+length(Fields))).YLabel.String,'m',sprintf('\\tau_{%d}',Fields(ik)));
% end
end
%%
% strrep(fh.Children(yLabelN(ik)).YLabel.String,'t_','t^{*}_{1}')
% rename x labels
ik = 1:length(xLabelN)
for ik = 1:length(Fields)
if strfind(fh.Children(xLabelN(ik)).XLabel.String,'sigma')
fh.Children(xLabelN(ik)).XLabel.String = strrep(fh.Children(xLabelN(ik)).XLabel.String,'sigma',sprintf('\\sigma_{%d}',Fields(ik)));
% indSig = indSig - 1;

end
if strfind(fh.Children(xLabelN(ik+length(Fields))).XLabel.String,'tau')
fh.Children(xLabelN(ik+length(Fields))).XLabel.String = strrep(fh.Children(xLabelN(ik+length(Fields))).XLabel.String,'tau',sprintf('\\tau_{%d}',Fields(ik)));
% indSig = indSig - 1;
end
end

Best Answer

  • Try this
    % your example code
    Fields = [1, 4, 5];
    Fields_time = Fields +16;
    MT_All = rand(100,26);
    % MT_All = rand(100,9);
    VariableNames={'sigma','sigma','sigma','tau','tau','tau','mNPV'}; % changed to ensure valid syntax
    Mat_All_1_4_5 = [MT_All(:,Fields), MT_All(:,Fields_time), MT_All(:,end-1)];
    figure
    c = corrplot(Mat_All_1_4_5, 'varNames', VariableNames);
    % get current figure handle
    fh = gcf;
    % find x and y label strings that are not empty within subplots
    yLabelN = find(cell2mat(arrayfun(@(dIn)~isempty(dIn.YLabel.String),fh.Children,'UniformOutput',false)));
    xLabelN = find(cell2mat(arrayfun(@(dIn)~isempty(dIn.XLabel.String),fh.Children,'UniformOutput',false)));
    % rename y labels
    % indSig = 0;
    % for ik = 1:length(yLabelN)
    %%
    for ik = 1:length(yLabelN)
    if ik <= 3
    fh.Children(yLabelN(ik)).YLabel.String = sprintf('\\sigma_{%d}',Fields(ik));
    fh.Children(xLabelN(ik)).XLabel.String = sprintf('\\sigma_{%d}',Fields(ik));
    elseif ik <=6
    fh.Children(yLabelN(ik)).YLabel.String = sprintf('\\tau_{%d}',Fields(ik-3));
    fh.Children(xLabelN(ik)).XLabel.String = sprintf('\\tau_{%d}',Fields(ik-3));
    else
    fh.Children(yLabelN(ik)).YLabel.String = sprintf('m_{NPV}');
    fh.Children(xLabelN(ik)).XLabel.String = sprintf('m_{NPV}');
    end
    end