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

128 ビュー (過去 30 日間)
LEONG SING YEW 2019 年 12 月 2 日

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 件のコメント表示 1 件の古いコメント非表示 1 件の古いコメント
LEONG SING YEW 2019 年 12 月 2 日
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?
i have attached both the temperature.csv and matlab data.m file in case you need...

サインインしてコメントする。

### 採用された回答

Rik 2019 年 12 月 2 日

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 件のコメント表示 2 件の古いコメント非表示 2 件の古いコメント
LEONG SING YEW 2019 年 12 月 4 日
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!

サインインしてコメントする。

### その他の回答 (1 件)

Vidya shree V 2020 年 9 月 24 日
I have x , y nad T values how to plot contour graphs in matlab

サインインしてコメントする。

### カテゴリ

Find more on Surface and Mesh Plots in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by