Load .mat file with string type variable

Hello,

I want to load a .mat file in the initialization commands of my plecs simulation.

Is it possible to load the file if it contains a string type variable (i.e. declared with config = “MMCA1” in matlab) ?

When I try to do that I get this error : “Initialization commands of model ‘test’ cannot be evaluated: save: error while writing ‘’ to MAT file.”.

When I remove the string variable it works well.

Thanks for your help

Hi @Paul

I tested this with both PLECS Standalone and PLECS Blockset.

For a MAT file, the string should be stored as a character array, for example:

config = 'MMCA1';
save('test.mat', 'config');

The file can then be loaded during initialization with:

load('test.mat');

This works with both PLECS Standalone and PLECS Blockset.

The reason is that in MATLAB

config = 'MMCA1';

creates a character array, while

config = "MMCA1";

creates a MATLAB string object. These are different data types. PLECS can read character arrays from MAT files, but MATLAB string objects are not supported by the PLECS MAT-file interface. Therefore, if the MAT file is intended to be loaded by PLECS, the value should be converted to a character array before saving, for example:

config = char(config);
save('test.mat', 'config');

An alternative is to put the initialization into an M-file and execute it with:

run('test.m');

For example:

config = 'MMCA1';

Using single quotes is also the portable choice here.

There is a small difference between Standalone and Blockset that can otherwise be confusing. PLECS Standalone evaluates the M-file using its Octave-compatible scripting environment, where double-quoted text can be handled as a character string. PLECS Blockset executes the M-file in MATLAB, where "MMCA1" creates the MATLAB-specific string type.

Therefore, if the same initialization file should work with both PLECS Standalone and PLECS Blockset, I recommend using:

config = 'MMCA1';

load_string_R2020b.slx (21.0 KB)
load_string.plecs (8.3 KB)
test.m (73 Bytes)

Hello,

Thanks for your response.

I will then convert my string variables to chars.

Does the matlab string type will be one day supported by plecs MAT-file interface (as well as complex numbers) ?

We do not currently have a committed timeline for adding support for MATLAB string objects or complex numbers.

For using complex numbers in the initialization script, this post might be helpful: Obtain PLECS parameter initial values from a .m file - #6 by Bryan_Lieblick