I am trying to programatically save a matrix as a .jpg or .png file. The output should be a jpg that looks like a spreadsheet, similar to an excel document. For example, if the matrix was:
1 2 3
4 5 6
7 8 9
The output of the program should look something like this (screenshot from excel) in a jpg or png file:
What function should I use to make this happen?
Thank you!

 채택된 답변

Adam Danz
Adam Danz 2022년 7월 13일
편집: Adam Danz 2022년 7월 13일

0 개 추천

Storing numeric data in images makes it difficult to work with those data later on but I remember a use-case for this back when I wanted to quickly snap-shot small data sets for visual reference (still not sure it was the best approach).
Nevertheless, if your matrix fits on a single page, you could put the data in a uitable and then extract the figure using exportapp. Note that precision may be lost since there is a finite number of decimal places that will appear on the table. See uitable documentation to learn how to apply column and row labels.
x = rand(10,5)*100;
fig = uifigure(); % must be uifigure
uit = uitable(fig,'Units','Normalize','Position',[0 0 1 1],'Data', x)
After the figure and uitable are rendered, you can export the figure as an image using exportapp.
% drawnow % only needed if calling exportapp immediately after uitable.
% pause(.5) % only needed if calling exportapp immediately after uitable.
exportapp(fig, 'mytable.png')

댓글 수: 7

Varun Kumar
Varun Kumar 2022년 7월 13일
The table generation is fine, but when I open the image up, it is blank. What can I do to fix this? Thanks!
Adam Danz
Adam Danz 2022년 7월 13일
Are you using uifigure() and not regular figure()?
Are you supplying the figure handles to uitable (uitable(fig,...))?
Are you using exportapp rather than exportgraphics?
I see you're using R2020b (thanks for filling out the release info). I just tested it in 2020b and it exports the table as a png as expected.
Varun Kumar
Varun Kumar 2022년 7월 13일
Yes, I just copied the code that you gave. Not sure why it's not working.
I see what's happening and was able to reproduce the problem.
The figure and uitable need to be rendered before exporting the image. If you run the entire code at once, exportapp is called too quickly before the uitable is rendered.
I'll update my answer to include the following two lines that will prevent this problem.
Place these lines before exportapp
drawnow
pause(.5)
drawnow alone did not solve the problem, the additional pause is necessary but the duration of the pause might be reducible.
Varun Kumar
Varun Kumar 2022년 7월 13일
This is great. Thank you very much! I'll accept this answer now.
Varun Kumar
Varun Kumar 2022년 7월 13일
Sorry, another issue just arose. What do I do if my matrix does not fit on one page? Can I resize it?
Thanks!
Adam Danz
Adam Danz 2022년 7월 13일
What is your matrix size?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2022년 7월 13일

댓글:

2022년 7월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by