MATLAB: How to do density plot

contourplot

suppose, I have a text file 'data.txt' that has three columns and contains output of below.
% code
fileID = fopen('data.txt','w');
for x = 0.1:0.1:10
for y = 0.1:0.1:10
z = x*y;
T = [x, y, z];
fprintf(fileID,'%f %f %f\n',T);
end
end
fclose(fileID);
Now i want to do the density plot of this data. Please help.

Best Answer

Try this vectorized approach with meshgrid():
% Generate the data.
xv = 0.1:0.1:10
yv = 0.1:0.1:10
[x, y] = meshgrid(xv, yv)
z = x .* y
T = [x(:), y(:), z(:)]
% Show the x,y,z data as a surface
surf(xv, yv, z, 'EdgeColor', 'none');
xlabel('x', 'FontSize', 20);
ylabel('y', 'FontSize', 20);
zlabel('z', 'FontSize', 20);
% Write the file.
filename = fullfile(pwd, 'data.txt');
fileID = fopen(filename, 'wt');
fprintf(fileID,'%f %f %f\n',T'); % Don't forget to transpose with '
fclose(fileID);
type(filename);
% delete(filename);