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.

댓글 수: 4

Adam Danz
Adam Danz 2018년 9월 19일
This code writes data to your data.txt file. Option 1 is to read the file to get the data back into matlab and then do the plotting. Option 2 is to save the data within this function/script so you don't have to read it back in. Depending on what you're trying to do, you may not even need to write it to the txt file in the first place (option 3).
Secondly, you need to specify what data you want to plot. For example, do you want to plot the density of each column of T separately?
Thirdly, how would you like to represent the density? Are you looking to plot a histogram?
sourabh mittal
sourabh mittal 2018년 9월 19일
편집: sourabh mittal 2018년 9월 19일
I want option 1. I want 1st column of T on x-axis and 2nd column on y-axis and then 2-D color density plot of 3rd column with a color bar.
Adam Danz
Adam Danz 2018년 9월 19일
I continued in the answer section though I'm unsure about the plot you're describing.
This is what i was searching for
% code
fileID = fopen('data.txt','r');
formatSpec = '%f';
A = fscanf(fileID,formatSpec);
x=A(1:3:end); y=A(2:3:end); z=A(3:3:end);
f=scatteredInterpolant(x,y,z);
[X,Y]=meshgrid(0.1:0.1:10,0.1:0.1:10);
Z=f(X,Y);
surf(X,Y,Z)
view(0,90)
colorbar
Thank you!

댓글을 달려면 로그인하십시오.

 채택된 답변

Image Analyst
Image Analyst 2018년 9월 19일

0 개 추천

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);

댓글 수: 3

sourabh mittal
sourabh mittal 2018년 9월 19일
편집: sourabh mittal 2018년 9월 19일
This is nice approach but I want to read the file to get the data back into matlab and then do the 2-D plotting.
Image Analyst
Image Analyst 2018년 9월 19일
OK. I thought you were going to visualize the data in the same program that created it. But it looks like Adam alerted you to the function dlmread(). But you might still look at my vectorized way of using meshgrid() - it's a handy function that allows you to do in one line what would otherwise be a multi line set of nested for loops.
Anyway, after calling dlmread(), you can use surf() like I showed, or scatter() or imagesc() like Adam showed, or even histogram() or histogram2(), depending on what you mean by density. Still even now, no one has a clear idea of what you mean by that, other than your acceptances of Adam's answer which I guess means you wanted either scatter() or imagesc().
sourabh mittal
sourabh mittal 2018년 9월 20일
Thanks my problem was solved by rotating surf by 90 degree.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2018년 9월 19일
편집: Adam Danz 2018년 9월 19일

0 개 추천

You can read the data back in with dmlread()
data = dlmread('data.txt', '%f');
' data' will be the [10000 x 3] matrix.
For the figure, I'm not sure if this is what you're looking for but you can try it.
figure;
imagesc(data(:,1), data(:,2), data(:,3));
colorbar
Another option
figure;
scatter(data(:,1), data(:,2), 10, data(:,3))

댓글 수: 1

Adam Danz
Adam Danz 2018년 9월 19일
See comments under the answer provided by @ImageAnalyst for more interpretations of what you'd like to plot in 2D space.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Scatter Plots에 대해 자세히 알아보기

태그

질문:

2018년 9월 19일

댓글:

2018년 9월 20일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by