如何将图形保存在MAT文件中? [英] How to save a figure in a MAT-file?
问题描述
我想将图像直接保存为256x256大小的MAT文件.但是,我发现保存的MAT文件大小不同,并且使用imagesc
显示图像时,它似乎与原始图像有些不同.我将展示我的代码,并希望有人可以帮助我解决它.
I want to save the image in a figure directly as a 256x256 size MAT-file. However, I found that the saved MAT-file sizes were different, and when using imagesc
to display the image, it seemed to be a little different from the original image. I will show my code and hope someone could help me to solve it.
spectrogram(x,window,L,N,fs);
set(gcf,'position',[500,500,205,205]);
set(gca,'Position',[0 0 1 1]);
f=getframe(gcf);
mat=getimage(gcf);
save(['D:\matlab\speech\mydata\cleanmat\',strcat(int2str(i)),'.mat'],'mat','-v6');
推荐答案
save
在这里没有做任何意外的事情.问题是y轴的方向相反.换句话说,图像像素是从左上角开始计数的,而绘图通常是从左下角开始绘制的.
save
doesn't do anything unexpected here. The issue is that the direction of the y-axis is inverted. In other words, the image pixels are counted from the left top whereas the plots are usually made from left bottom.
如果您在代码中删除了此行set(gca,'Position',[0 0 1 1]);
,您将能够看到它.
If you remove this line set(gca,'Position',[0 0 1 1]);
in your code, you'll be able to see this.
注意以下图中突出显示的部分.
Notice the highlighted parts in the following plots.
使用其文档中的代码绘制的频谱图:
Spectogram plotted using the code from its documentation):
imagesc(mat);
给出了:
那该如何解决?
只需反转y轴方向即可.
So how to fix this?
Just reverse the y-axis direction i.e.
imagesc(mat);
set(gca,'YDir','normal');
结果:
这篇关于如何将图形保存在MAT文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!