How to save a figure in a MAT-file?

允我心安 提交于 2019-12-02 11:29:16

问题


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

回答1:


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.

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

While, the imagesc(mat); gives:


So how to fix this?
Just reverse the y-axis direction i.e.

imagesc(mat);
set(gca,'YDir','normal');

Result:



来源:https://stackoverflow.com/questions/57933537/how-to-save-a-figure-in-a-mat-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!