Printing multiple shapes in matlab

Suppose I produced a few digits in my program. I want to give the user the opportunity to print them all at once. I do not want to show a print dialog for each page. Therefore, I show it only once and only for the first day. This is the solution I came up with so far:

figHandles = get(0, 'Children'); for currFig = 1:length(figHandles) if(currFig == 1) printdlg(figHandles(currFig)); % Shows the print dialog for the first figure else print(figHandles(currFig)); % Does not show the print dialog and uses the latest printer selection end end 

But the problem is that if the user cancels the print for the first digit, I can’t catch it and cancel the other prints. How do I achieve this?

+5
source share
1 answer

Well, this is a pretty dirty trick, and there is definitely no guarantee that it will work for all versions. This works for me on Matlab 2013a / win 7.

To return Matlab to a print job or not, you need to insert a small hack into the print.m function.


print.m

  • Find the function print.m . It should be in the Matlab installation folders near ..\toolbox\matlab\graphics\print.m .

  • After posting, make a backup! (this trick is insignificant and should not break anything, but we never know).

  • open the file print.m and find the line LocalPrint(pj); , it should be near or at the end of the main function (~ line 240 for me).

  • Replace the line with:

.

 pj = LocalPrint(pj); %// Add output to the call to LocalPrint if (nargout == 1) varargout{1} = pj ; %// transfer this output to the output of the `print` function end 
  • Save the file.

Made for hacking. Now every time you call print , you can get a return argument full of information.


Applicable to your case:

First, note that on Windows machines, the printdlg function printdlg equivalent to calling the print function with the argument '-v' .
So printdlg(figHandle) exactly matches print('-v',figHandle) . ( '-v' means verbose ). We will use this.

The output of the print function will be a structure (let it be called pj ) with many fields. The field you want to check to see if the print command was actually executed is pj.Return .

 pj.return == 0 => job cancelled pj.return == 1 => job sent to printer 

So, in your case, after setting up on print.m , it might look like this:

 pj = print('-v',figHandles(1)); %// Shows the print dialog for the first figure if pj.Return %//if the first figure was actually printed (and not cancelled) for currFig = 2:length(figHandles) print(figHandles(currFig)); %// Does not show the print dialog and uses the latest printer selection end end 

Note. The pj structure contains much more information for reuse, including print job parameters, currently selected printers, etc.

+1
source

Source: https://habr.com/ru/post/1204349/


All Articles