MATLAB: How to plot a correlation matrix between two variables as a heatmap in matlab

correlation matrixMATLAB

I have two variables which vary with each run of my code. I have to calculate the correlation matrix between these two variables. The matrix axes will be like variable X(first run),variable Y(first run),Variable X(second run),variable Y (second run)……I need to plot this correlation matrix as a heat map?
please help me

Best Answer

Just perform Pearson or Spearman correlation depending on your data. Then display the resulting matrix as an image and apply any colormap of your choice.
%% Code Snippet
corr(yourvariables)
imagesc(ans); % Display correlation matrix as an image
set(gca, 'XTick', 1:sizeofyourcorrmatrix); % center x-axis ticks on bins
set(gca, 'YTick', 1:nsizeofyourcorrmatrix); % center y-axis ticks on bins
set(gca, 'XTickLabel', yourlabelnames); % set x-axis labels
set(gca, 'YTickLabel', yourlabelnames); % set y-axis labels
title('Your Title', 'FontSize', 10); % set title
colormap('jet'); % Choose jet or any other color scheme
colorbar on; %