Loading and Saving a Model
Loading a Model at the MATLAB Prompt
To load an existing model saved as an MPH-file use the function mphopen. To load the model with the name <filename> enter:
model = mphopen(<filename>)
where <filename> is a string. This creates a model object Model on the COMSOL server that is accessible using the MATLAB variable model.
A shorter form is to simply use
mphopen <filename>
that will load the model with the given filename and use the variable name model for accessing the model later. Any existing variable model will be overwritten without warning.
The function mphload can also be used with the same property. In the following documentation the commands also work with mphload.
mphload does not store the filename in the recent file list as mphopen does by default.
Once the model is loaded, the file name and its associated model object tag are displayed in the COMSOL server window.
If there is already a model object Model in the COMSOL server, mphopen overrides the existing model object unless the model is also open in a COMSOL Multiphysics Client. In the later case, an index number is appended to the new model object name, for instance Model1.
mphopen and mphload do not look for lock file when opening a model in the COMSOL server.
If you want to manually specify the model object in the COMSOL server. use the command:
model = mphopen(<filename>, <ModelTag>);
where <ModelTag> is a string defining the tag that defines the loaded model in the COMSOL server.
When using the function mphopen, the model history is automatically disabled to prevent large history information when running a model in a loop. To turn model history on, use the function mphopen:
model = mphopen(<filename>,'-history');
If you do not want to update the recent opened file list with the model you are about to open, use the -nostore flag with the function mphopen as in the command below:
model = mphopen(<filename>, <ModelTag>, '-nostore')
If the model mph-file is protected using a password, use mphload as in the command below:
model = mphopen(<filename>, <ModelTag>, <password>)
where <password> is a string defining the password protecting the file.
If you want to get the full filename of the loaded file, add a second output as in the command below:
[model, filenameloaded]= mphopen(<filename>, ...)
Loading a Model from a List of Existing files
You can use a GUI where to load the model from a list files corresponding to the recent opened file or the files in a specified directory.
At the MATLAB prompt enter the command:
mphopen
This starts a GUI with a list of the recent opened files.
For each selected files, the model information is available in the File Info section.
Click the Recent button to get the list of the recent opened file. Click the Search button to search for a file using file pattern. Click the Browse button to browse the directory where to get filename list.
To clear the recent opened file list enter the command:
mphopen -clear
To open the GUI with the list of files in a specific directory (<dirpath>), enter the command:
mphopen -dir <dirpath>
Saving a Model
Use the function mphsave to save the model object linked to the MATLAB object model:
mphsave(model,<filename>)
where <filename> is a string. If the filename specified <filename> does not provide a path, the file is saved relative to the current MATLAB path. The file extension determines the format to use (*.mph, *.m, *.java, or *.vba).
Alternatively, use the save method:
model.save(<filename>)
If <filename> does not provide a path, the file is saved relative to the local server path.
Any files saved in the MPH format can be loaded by COMSOL Desktop. In addition, the model can be saved as an M-file:
model.save(<filename>,'m')
When saving the model as an M-file mphsave does not automatically use the component syntax to save model using the COMSOL API, to enable this syntax run the command below:
mphsave(model,<filename>,'component','on')
Set a model Thumbnail
Before saving your model, you may want to include a model thumbnail to quickly identify your model in your own Application Library or when using mphopen. To set the model thumbnail enter the command:
mphthumbnail(model,<filename>)
where <filename> is the image file name.
You can also use a MATLAB figure to set the thumbnail. The following command will set the thumbnail to the image of the current figure:
mphthumbnail(model,gcf)
Note that the thumbnail is stored in memory. In order to save the thumbnail in the model file the model must be saved.
You can extract the image and image filename for the thumbnail stored in model, enter the command:
[image, imagefilename] = mphthumbnail(model)
Example
The code below shows how to get the model thumbnail as MATLAB image data, show the image in a MATLAB figure and store the new image as thumbnail in the model.
mphopen model_tutorial_llmatlab
im = mphthumbnail(model);
imshow(im)
mphthumbnail(model, gcf)