# Matlab的2维值图

### 输入数据:

• 这里输入数据格式采用sparse数据格式,前两列为坐标,第三列为值.如:
``````1 1 2
1 2 4
...
1 667 9
2 1 5
2 2 8
...
1001 667 1
``````

• 对于规则型如这个数据1001*667全备的,可以用紧凑型数据,第一行指明维度,第二行第一维的数量NX,第三行是第二维的NY,如此类推.这样数据全读入后,用reshape()函数变换矩阵即可.

###matlab脚本 脚本如下:

``````clear all;
clc;

% part1: read data
filename='emd_1149-2Dtimesum_0.sdat';
[dirr,filee,extt]=fileparts(filename);
A=load(filename);

% part2: calculate total frames on each axis
imin=min(A(:,1));
jmin=min(A(:,2));
imax=max(A(:,1));
jmax=max(A(:,2));
deltaJ=A(2,2)-A(1,2);
times=abs((jmax-jmin)/deltaJ)+1;
deltaI=A(times+1,1)-A(1,1);
frames=abs((imax-imin)/deltaI)+1;

% part3: construct the 2D-matrix to save the values.
B=zeros(frames,times);
for i=1:frames
for j=1:times
B(i,j)=A((i-1)*times+j, 3);
end
end

% part4: make a Figure
handleN=imagesc(B');
%handleN=imagesc(B(1:500, :)');
title(strrep(filee,'_','-'));
xlabel('Density');
ylabel('Time');
%reverse the Y axis
set(gca,'YDir','normal');
%set up the colorbar range
%set(gca, 'CLim', [0 20]);
colorbar
saveas(handleN, [filee,'.png'], 'png');
``````
• `fileparts`函数用来对文件名分析.
• part2 计算出每列最值和interval,算出帧数
• `imagesc`用来出图,这里对矩阵转置,注释掉一句用于出局部数据.
• `strrep`为了防止标题中`_`会作为下标指示符而进行替换.
• `set (gca,'YDir','normal')`一句用来反转Y轴坐标.
• `set(gca, 'CLim', [0 20]);`一句用来控制colorbar的值范围.
• `colorbar`用来出色带.

I don’t know why the matlab syntax highlight can’t work here… I may check it in future. My prism.js in TOC.js doesn’t contain the matlab. The matlab in prism is wrong. It will take all things as comments.The comment should be written as:
`comment:{pattern:/(^|[^\\])%.*?(\r?\n|\$)/g,lookbehind:!0}`

◆ 本文地址: http://platinhom.github.io/2015/06/09/Matlab-2Dvalue-plot/, 转载请注明 ◆

/ 已阅读()
Source 类别: MathStat