1

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');
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
yunyang
  • 43
  • 4
  • 1
    The size of the MAT-file is irrelevant. Check the size of the variables stored in it. MAT-files use lossless compression, meaning different matrices of the same size require a different number of bites to be stored. – Cris Luengo Sep 14 '19 at 14:06
  • 1
    I would recommend that you do `mat = spectrogram(...)`, then save `mat`. By avoiding the figure, your code is simpler and likely more consistent. – Cris Luengo Sep 14 '19 at 14:12
  • In your opinion, the code should be modified to :mat=spectrogram(x,window,L,N,fs);save(['D:\matlab\speech\',strcat(int2str(i)),'.mat'],'mat','-v6'); It that right? However, such generated mat files cannot be displayed using imagesc.This is different from the data provided by the original author. – yunyang Sep 14 '19 at 14:23
  • 1
    Why can they not be displayed? Why is the data different? I would say capturing the displayed data loses a lot, since you’re limited to 256 different colors in the color map. Saving the original data is **always** better. Just figure out how `spectogram` does the display, that can’t be hard. – Cris Luengo Sep 14 '19 at 14:28

1 Answers1

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:

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
  • Thanks for your answer.My spectrogram is unset by 'yaxis' and drawn by default. Because the author of pix2pixSE gave the same data as this. However, what i really want to know is how to directly save the drawn language spectrum into mat files of 256*256 size instead of jpg files. – yunyang Sep 14 '19 at 13:20
  • @yunyang A mat file doesn't have a size like you mentioned. mat files have variables inside them. A variable can be a 256*256 matrix. You are supposed to save the mat file using exactly the same way that you've shown in the question. In your question, you have mentioned that the problem is that you are getting a little different image when you load your mat file and show it with `imagesc`. I have explained why this is happening and how to fix this – Sardar Usama Sep 14 '19 at 13:27
  • Does the variables in the mat matrix could be set? Which means that all spectrogram could be saved as mat files with 256*256 matric.The codes in my questions save different spectrograms into different mat files which have various kind of matrix. – yunyang Sep 14 '19 at 13:54
  • @yunyang It seems that you have your code in a loop in which you are saving multiple mat files. Are you asking how to save a single mat-file to include the results of all iterations? If yes then make `mat` variable a 3D matrix to include all the iterations of your loop and then save the mat-file outside the loop. But then this is different from your original question. If original query is solved then please close this question (See how to do here: https://stackoverflow.com/help/someone-answers) and ask another question by clicking the [Ask Question](//stackoverflow.com/questions/ask) button. – Sardar Usama Sep 14 '19 at 14:07