Multiple errors in export_fig, "invalid figure handle specified to export_fig"
18 views (last 30 days)
Show older comments
Hello,
I'm working with a programm to track the movement of a particle. Sadly I'm pretty new to matlab and don't have a great understanding for the code and the program is an old program from another PhD student.
At the end of the program it should use export_fig to export the data, I think is this case a GIF created from the frames of the input video, where the particle is marked. I got the export_fig function here from mathworks.com, but it displays some errors:
Error using export_fig>parse_args (line 1576)
invalid figure handle specified to export_fig
Error in export_fig (line 392)
[fig, options] = parse_args(nargout, fig, argNames, varargin{:});
Error in Rolling_one_single_Particle_2 (line 137)
export_fig(I2,['W:\Kamera pco 1200hs\Testaufnahmen',num2str(l),'.gif']);
The first two errors are not even in my code, but in the downloaded one and I don't know what exactly is wrong or how I can fix ist. Googeling the errors didn't help me either.
The third error is just the code from the export, but sadly it doesn't say what the error ist, just that something is wrong.
Did anyone encounter those errors and/or has a solution for them?
Thanks in advance,
Simon
0 Comments
Answers (1)
Image Analyst
on 14 Feb 2023
Edited: Image Analyst
on 14 Feb 2023
Chances are I2 is not a figure handle. So you didn't get I2 by doing this:
I2 = figure();
I don't know what it is, but it's not a figure handle. Perhaps it's an image and you should use imwrite() instead of export_fig().
folder = 'W:\Kamera pco 1200hs\';
fullFileName = sprintf('Testaufnahmen %d.png', l);
export_fig(gcf, fullFileName); % Or gca or a valid figure or axes handle.
If you really still want to use export_fig, then ask @Yair Altman
3 Comments
Image Analyst
on 15 Feb 2023
@Simon Wrana export_fig can handle images as long as they are in an axes container. If you just have a bare image variable, like I or I2, then the way to save the image is via imwrite(). Images don't even need to be displayed if you are calling imwrite, though of course they can be.
However if you have graphics in the overlay and/or want tick labels, axes labels, and axes titles to also be saved with the image then you'll need to save the axes or the figure, not the image variable. So for that (to save additional graphics) you need to give export_fig the figure handle or axes handle.
Yair Altman
on 15 Feb 2023
export_fig requires a handle as input, not a numeric image matrix data. The purpose of export_fig is to export whatever you currently see in a figure to an external file (e.g. pdf or png file). It says so at the very top of the function's documentation. As the name of the function implies, export_fig exports a fig(ure), and what you gave it was not a figure. export_fig does not know what to do with numeric matrix data, because this is not a graphics handle.
If you want to use export_fig, you can use functions such as image() to display your data in a figure, and then use export_fig with the handle oof that figure.
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!