Newsletters - MATLAB Digest
Troubleshooting tips for P-coding a MATLAB Runtime Server application
by Shannon Hays and Peter Torrione
The MATLAB Runtime Server enables you to redistribute MATLAB applications without requiring end users to have MATLAB installed. This article expands upon one of the examples shipped with the Runtime Server, showing you how to print from the completed application. The article discusses some of the common problems encountered while developing a Runtime Server application and provides troubleshooting tips for resolving these problems.
Modifying myapp.m to add printing functionality
The MATLAB Runtime Server ships with several examples. We will modify one of these examples, MYAPP, to add printing functionality. In doing so, we will show how complicated callbacks can affect the p-coding of files. You can find myapp.m in $MATLAB\toolbox\runtime\examples\gui or download the example (4k).
If you download the example, follow the instructions in the MATLAB Runtime Server Application Developer's Guide, pages 2-30, to install the files in the proper directories.
Make sure you have stamped your copy of MATLAB with a password using RTSETUP. Please see pages 1-5 in the MATLAB Runtime Server Application Developer's Guide for an example and further instructions.
First, add a new menu item to the GUI. Edit myapp.m by adding the following command to the end of the file:
printmenu = uimenu('Parent',filemmenu,'Callback','myapp_print','Label','&Print',...
'Accelerator','P');
Next, create a file called "myapp_print.m" that includes the following lines:
function myapp_print
print
The above edits will enable you to print directly from the M-file. However, if you also want to print from the stand-alone application using this code, and you follow the directions in the MATLAB Runtime Server guide (pages 2-40) for P-coding the M-file, you will see errors regarding missing P-files when you test in RUNTIME mode.
For example:
Error loading D:\MATLAB\toolbox\matlab\graphics\@printjob\validate.m. M-files cannot be run in Runtime MATLAB.
Error in ==> d:\MATLAB\toolbox\runtime\examples\gui\myapp_print.m On line 3 ==>
??? Error while evaluating uimenu Callback.
Missing P-files
The most common problem encountered when using the MATLAB Runtime Server is not P-coding all of the files that your application requires. The Runtime Server comes with a utility called DEPFUN, which searches the MATLABRT file and provides a list of all of the MATLAB functions that your application calls. However, because DEPFUN cannot parse some function calls, it may not list some of your application's functions. For example, since UICONTROL callbacks are often written as strings, DEPFUN may not be able to determine which functions your UICONTROL requires.
There are several ways to solve the missing P-file problem. You can use the INMEM function to produce a list of all of the M-files called in the current MATLAB session. If you use all the features of your GUI in the current session, all of the M-files will be listed in the INMEM output, allowing you to determine which files (if any) need to be P-coded. This method is explained in more detail in the next section.
Alternatively, you can P-code all of the files that you think are required, then use the RUNTIME ON command to enter Runtime Server Emulation mode. Whenever an M-file is required that hasn't been P-coded, you will see an error message, telling you the name of the file you need to P-code.
Finding functions that require P-coding
You can use the DEPFUN command together with the INMEM command, to find functions that require P-coding.
1. Use DEPFUN to create a list of all the functions that MYAPP calls:
newlist = depfun('matlabrt')
2. Restart MATLAB and run MYAPP.
3. Draw a plot and print it from the File->Print menu.
4. After you are done, click "Erase" and type
memlist = inmem
This will produce a long list of the functions called since MATLAB was started. The list should look like the output below. The order in which the files are listed in memlist depends on your exact path and the functions you used since MATLAB was started.
'D:\MATLAB\toolbox\matlab\graphics\@printjob\private\noselection'
'printjob/blt'
'savtoner'
'printjob/prepareui'
'D:\MATLAB\toolbox\matlab\graphics\@printjob\private\screenpos'
'printjob/preparehg'
'printjob/subsasgn'
'D:\MATLAB\toolbox\matlab\graphics\@printjob\private\setset' 'D:\MATLAB\toolbox\matlab\graphics\@printjob\private\getget' 'printjob/subsref' 'D:\MATLAB\toolbox\matlab\graphics\@printjob\private\isslhandle' 'D:\MATLAB\toolbox\matlab\graphics\@printjob\private\isfigure' 'D:\MATLAB\toolbox\matlab\graphics\@printjob\private\ishghandle'
'printjob/tables'
'printjob/restorepointers'
'printjob/finish'
'printjob/restore'
'printjob/render'
'printjob/prepare'
'printjob/positions'
'printjob/start'
'printjob/setup'
'printjob/name'
'printjob/validate'
'printjob/preparepointers'
'printjob/inputcheck'
'printjob/printjob'
Note that there are several M-files listed in memlist that are not in newlist.
All of the functions in memlist that were not in newlist need to be P-coded as well. In general, if you have a simple GUI (as in MYAPP) where it is reasonable to try out every combination of commands, you can use INMEM to determine every function that you will need to P-code for a stand-alone program.
5. Next, P-code the newlist that you created in Step 1.
Pcode('-runtime', newlist{:})
6. Comparing the two file lists, you will see the following files in memlist that are not in newlist:
$MATLAB\toolbox\matlab\graphics\@printjob\restorepointers.m $MATLAB\toolbox\matlab\graphics\print.m $MATLAB\toolbox\runtime\examples\gui\myapp_print.m $MATLAB\toolbox\matlab\uitools\findall.m $MATLAB\toolbox\matlab\graphics\@printjob\*.m $MATLAB\toolbox\matlab\graphics\@printjob\private\*.m $MATLAB\toolbox\matlab\graphics\savetoner.m
$MATLAB represents your root MATLAB directory.
7. Next, P-code each of the files in the above list.
Pcode('-runtime', memlist{:},'-inplace')
8. To test the application, please refer to page 2-40 in the MATLAB Runtime Server guide "Testing in Runtime Mode." While debugging, it is easy to use emulation mode "runtime on". However you should test the application with the MATLAB Runtime Server. More information can be found on page 2-22 of the Guide.
Chapter 4 in the MATLAB Runtime Server Application Developer's Guide has instructions for shipping the Runtime Application that we created, using the PACKAGE command.
Store