hey assistent, sometimes I get this…
- User
hey assistent, sometimes I get this print(hFigureLegend,'-dpng','-r350','-loose',fullfile(nomRepertoire,nomFile)); for % Plot & Print legend if ~isempty(hFigureLegend) print(hFigureLegend,'-dpng','-r350','-loose',fullfile(nomRepertoire,nomFile)); pause(0.1); end. I guess this is becous hFIgureLegend doesn't exist. can you make a try catch or change the if stametent if hFigure dosen't exist, do nothing
- Assistant
Certainly! If you want to check the existence of
hFigureLegendbefore attempting to print, you can modify your code with atry-catchblock or by adjusting theifstatement to ensurehFigureLegendexists and is valid. I'll show you both approaches:Approach 1: Using
try-catchmatlabtry if isgraphics(hFigureLegend) % Verify if hFigureLegend is a valid graphics object print(hFigureLegend, '-dpng', '-r350', '-loose', fullfile(nomRepertoire, nomFile)); pause(0.1); end catch err fprintf('An error occurred: %s\n', err.message); % Optional: Additional error handling can be placed here end - User
I don't need erorr handeling, if it exists do print, if not do nothing
- Assistant
Got it! If you want to simply print when
hFigureLegendexists and is a valid figure, without having error handling, you can use just theifstatement. Here's the simplified version of your code:matlab% Check if hFigureLegend exists in the workspace and is a valid graphics object if exist('hFigureLegend', 'var') && isgraphics(hFigureLegend) print(hFigureLegend, '-dpng', '-r350', '-loose', fullfile(nomRepertoire, nomFile)); pause(0.1); endExplanation
-
exist('hFigureLegend', 'var'): This part checks whetherhFigureLegendhas been defined. -
isgraphics(hFigureLegend): This function verifies thathFigureLegendis a valid graphics object before proceeding with the print operation.
Using this combination ensures that you only attempt to print if
hFigureLegendexists and is valid, thus avoiding any errors that can arise when is not present or not a proper graphics object. If these conditions aren't met, the code simply does nothing and continues without any additional handling. -