How to plot a temperature in xy axis (2D temperature contour)?

111 views (last 30 days)
Hi i am new to matlab and I wanted to plot the temperature in x,y as shown above. I have set of data in excel file.
I've tried using surf plot but i keep getting errors "Z must be a matrix, not a scalar or vector."
  2 Comments
Rik
Rik on 2 Dec 2019
Surf will plot a surface where the complete grid of combinations of x and y are known. Is that the case for you? If not, you can probably interpolate your data to a complete grid.
Note that usually the z value determines the color, so if you have more than one z for the same x y, you should explain what exactly you expect to be your output. Also, attaching your data in a mat file may be helpful.
LEONG SING YEW
LEONG SING YEW on 2 Dec 2019
Thank you for the quick response, i only have one column of the z value (temperature), I notice that the data in my excel has what you mean "for than z for the same x,y...", how should i plot this?
Capture.PNG
i have attached both the temperature.csv and matlab data.m file in case you need...

Sign in to comment.

Accepted Answer

Rik
Rik on 2 Dec 2019
Edited: Rik on 4 Dec 2019
The surf function expects a matrix, so you will have to convert your linear data to a matrix. The accumarray function is a very flexible tool and worth exploring if you've never seen it before. A general advice would be to read the documentation for functions you are trying to use, and read the related functions linked at the bottom if you think it doesn't quite do what you need.
The code below should result in a plot you expect. Play around with the syntax for surf and colorbar to get the exact output you want.
%read data
filename='Temperature.csv';
Data = csvread(filename,6,0);%skip 6 header lines
X_data=Data(:,1);
Y_data=Data(:,2);
T_data=Data(:,4);
%create grid
[x,~,xi]=unique(X_data);
[y,~,yi]=unique(Y_data);
subs=[xi yi];%use the indices instead of values as coordinates
val=T_data;
M=accumarray(subs,val,[],[],NaN);%fill missing with NaN
[X,Y]=ndgrid(x,y);%generate the grid surf() expects
%create plot
figure(1),clf(1)
surf(X,Y,M)
%overlay the raw data points (optional)
% hold on
% plot3(X_data,Y_data,T_data,'*')
% hold off
view(0,90),xlabel('x'),ylabel('y')
colorbar
  3 Comments
Liping Ai
Liping Ai on 4 Dec 2019
It is clear that the 4th column is a temperature vector. What does the data in the 1st and 2nd column represent? Are they the coordinate vectors of an area?
LEONG SING YEW
LEONG SING YEW on 4 Dec 2019
Yes the result is exported from Fluent, 4th column is temperature, column 1 and 2 are x,y coordinates. Thanks to Rik, now i can present it to my professor like magic!

Sign in to comment.

More Answers (1)

Vidya shree V
Vidya shree V on 24 Sep 2020
I have x , y nad T values how to plot contour graphs in matlab

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!