Why can't Matlab R2010 load neural network objects from R2007? - matlab

I have a Neural Network that I've save to a .mat file in Matlab 2007.
I'm trying to load the neural network from the file with load filename.mat.
This worked great with versions R2007b and R2008, but when I try to load in R2010b I get the following message:
Warning: An error occurred when running a class's
loadobj method. The object that was loaded from the
MAT-file was a copy of the object before the loadobj
method was run. The rest of the variables were also
loaded from the MAT-file.
The encountered error was:
Reference to non-existent field 'name'
The weird thing is that it appears as if the variable holding the Neural Network exists, but it's not working properly. None of the functions that use the neural network work in R2010.
Has anyone encountered a similar problem ? How can I save the Neural Network so it will be compatible with Matlab R2010?
Or even better, how can I just load it properly in Matlab 2010?

Yes, it seems other people have encountered this problem (neural networks not loading in MATLAB 2010), according to this MATLAB Central post, in which Andreas Goser (Technical Support Manager at Mathworks) suggests:
If you see this issue in the future, please contact Technical Support and refer to solution 1-EGNM3S.
Apparently you need "a fixed net.m file" which technical support can provide. I haven't tested it, since I don't have that problem myself.
Carefully reading the error message, I would say that the variable you see loaded contains the "raw" object that was stored in the .MAT file, quite possibly a struct. You can run whos myvarname to check. To "revive" this struct into an object it needs to be processed by the overloaded loadobj function for its object type, which is usually automatically called by MATLAB's load function. In this case the loadobj that comes with the neuronal network object in R2010 seems to not be fully backwards compatible...

I suspect that the underlying objects for the neural net have changed since 2007b and that the definition in R2010b is not compatible with the data stored in the MAT-file.
I recommend following Jonas's answer. Alternatively, you might try loading it and re-saving it in an intermediate MATLAB release, or saving the data from the neural net objects and rebuilding it in R2010b.

Related

Initialize buildable Matlab Simulink Model with parameters from SQLite Database

Context: I have a huge Simulink Model that is going to be used for automated simulations on a Debian 10. Therefore it has to be built as standalone C-Code using the Matlab Coder. This code is then called to start the simulation.
What I need: I need to find a way to initialize my built model with ~500 parameters. These change with each simulation run and are stored in a SQLite file. The goal is to have parameters written to the database, then start the Model which reads the parameters from SQLite during initialization (presumably using the InitFcn Model Callback, although I'm open to alternatives).
What I have tried:
Direct SQL interface: I tried to use a direct Matlab-SQL interface such as JDBC (since I don't have access to the Database-toolbox) but those are not supported for Code generation.
Write a C-function that reads the SQLite file, then call the function during initialization in the InitFcn Callback using coder.ceval like this:
data = 0;
err = coder.ceval('read_function',4, 2, 12, coder.wref(data));
parameter = data;
Problem here is that coder.wref is not supported in Matlab and therefore doesn't work in the InitFcn. (Please correct me if I'm wrong)
This only seems to work inside a Matlab-Function-Block:
Error evaluating 'InitFcn' callback of block_diagram 'Model'.
Caused by:
The coder.wref function is not supported in MATLAB.
So my problem with the second approach is, that I can't call the C-function during initialization.
Using a Matlab-function-Block to read the parameters isn't really an option, since I would have to route all the signals out which makes maintaining and further development of the model really hard. Also my suggestion is, that the model would not even run because the parameters are needed to initialize the model.
Questions:
Is there a way to make one of the above approaches work? If yes, how? Where is my mistake?
Is there another (simpler) option to pass the data as an array or struct to my model?
Database looks like this:
Identifier Default
latitude 52.5
longitude 13.4
electricity_consumption 4000.0
ventilation_stream 50.0
PV_peak 30.0
PV_orientation 0.0
no_vessels 28.0
heatpump_exists 1.0
hotwater_consumption 1000.0
.
.
.
After having spent so much time on this issue, I would like to share my experience on this problem:
SQLite: This approach did not work out for me because the direct SQL-Matlab interfaces are not supported for code generation.
It is in fact possible to write a C-function, that reads from SQLite and call that function in a Matlab-function-block via coder.ceval wich allows to read in a signal during simulation. This works for code generation (Simulink coder) as well. However this will not work for initialization (see question).
So none of my original approaches ended up working.
Workaround: I ended up switching to an approach based on the Simulink RSIM-target wich generates code (also for Linux) and can be parametrized via a .mat file wich contains all the parameters. The .mat file can be modified to update parameters. This required some additional code wich automates this step. Also the model configuration for RSIM is a bit tricky.

Matlab Compiler: issues compiling code that includes a pre-trained neural netowork

I have a Matlab script that I want to compile in a standalone executable using Matlab Compiler. The code loads a couple of pre-trained networks that I have tailored on my problem, starting from Inceptionv3. The code runs OK before compilation, but when I run the compiled app, I've got an error as if it was not possible to load the pre-trained networks (.mat files).
I've searched on the web and on the official documentation, but there is a lack of clear response on this. I made myself sure that the problem is not due to the relative/absolute path.
There are suggestions on the web regarding the use of the command "%#function", but, by reading the documentation, it seems not to be what I need.
Here is when I load the models:
% load pre-trained network
model1 = load(fullfile(model_abspath,'Inceptionv3_net1.mat'));
model2 = load(fullfile(model_abspath,'Inceptionv3_net2.mat'));
net1 = model1.Inceptionv3_net1;
net2 = model2.Inceptionv3_net2;
Later on, in the code, I classify images using, for example:
[YPred,probs] = classify(model1.Inceptionv3_net1,img);
This is the error I got, and it happens when I try to load the models:
"Warning: Unable to load instances of class nnet.inceptionv3.layer.ScalingLayer into a heterogeneous array. The definition of nnet.inceptionv3.layer.ScalingLayer could be missing or contain an error. Default objects will be substituted.
Warning: While loading an object of class 'DAGNetwork':
Array indices must be positive integers or logical values."

Run Simulink from Matlab function

I am running Simulink using FastRestart, as I need to start and stop the simulation multiple times changing parameters. When I run Simulink from the main script, there are no problems. However, as soon as I make the script a function so that I can run it for different input data, I get an error that is clearly related to Simulink not seeing the Matlab workspace within the function.
To be more precise, say sfile is my Simulink file, then I run the following lines AFTER having initialized all variables I need in structures in Matlab:
load_system(sfile);
set_param(sfile,'FastRestart','on');
set_param(sfile,'SaveFinalState','on');
set_param(sfile,'SaveCompleteFinalSimState','on');
set_param(sfile,'SimulationCommand','update');
At the last line, I get the error that Simulink does not recognize mdl.tStep (which is the time step), as mdl is not a recognized structure. In fact, it is and if I run Simulink from the main script everything is fine.
Now, in the past, I would have used
options = simset('SrcWorkspace','current');
However, an expert I know has advised me against simset (as it may get deprecated in the future) and encouraged me to use set_param instead. I have
looked up the options for set_param on-line, but I could not find the setting for the Matlab workspace.
Any help would be greatly appreciated. Thank you in advance!
In many instances it is better to use the Model Workspace rather than the Base Workspace:
hws = get_param(model, 'modelworkspace');
hws.assignin('mdl',mdl);
At least be aware that this option exists.
A solution to your problem might be to use the assignin-function to all the variable whose value you want to pass to simulink in your matlab base workspace. To do so just use
assignin('base','mdl',mdl)
Having the variable in your base workspace should allow simulink to see it.

error when setting PostCodeGenCommand for a simulink model

I would like to add a post-build hook to my code generation process. However, when I try to associate a function with the simulink model's PostCodeGenCommand, I get the following error.
set_param(bdroot, 'PostCodeGenCommand', 'packNGo(buildInfo);')
A configuration set reference does not allow writing to parameters in the source configuration set
I don't really understand the error message, and it doesn't bring up any useful hits on my favourite search engine. Can anyone help me decipher it?
Unfortunately, I could not reproduce this problem on a simple simulink model, even if I included a reference model. (I thought that might be what the word "reference" was, um, referencing in the error message. And I've noticed previously that referenced models are handled differently to other models in the simulink code generator.)
I'm using R2015a, but have access to the newer versions if that would help.
Note that this question was originally posted on the Matlab forum, but didn't get any answers.
It turns out that the model was using a "configuration reference" rather than storing its configuration internally. The Model Explorer provides the following description
Configuration Reference
A model may reference a 'Configuration set' that is defined in the
source location rather than stored in the model....
This is useful for us because we use several models which all share the same configuration set. It's saved in a central place and all updates apply immediately to all models.
The side effect is that bdroot no longer has a PostCodeGenCommand associated with it. Instead, the command I was looking for was
configurationSettings.set_param('PostCodeGenCommand', 'packNGo(buildInfo);')
where configurationSettings is a variable in your workspace, and the variable's name should match the "Referenced configuration set name" in the Model Explorer.

Open the m file for System Objects in MATLAB

I am trying to use the Communications Toolbox in Matlab. In this toolbox there are a number of built in Systems Objects for example
1) comm.PSKModulator
I want to examine the .m file and see how these system objects are implemented. So I wrote down the command
open comm.PSMModulator
However, that doesnt help. Does anyone know why it doesn't work or maybe one cant access such code?
Update
When I write down which comm.PSKModulator I receive the following
/Applications/MATLAB_R2014a.app/toolbox/comm/comm/+comm/PSKModulator.p % comm.PSKModulator constructor
and where I write open comm.PSKModulator I get
Error using open (line 146)
File associated with
'/Applications/MATLAB_R2014a.app/toolbox/comm/comm/+comm/PSKModulator.p' not found.
Thanks
Some System objects may be implemented in C++ and does not have much to show in MATLAB code which might be the reason this is p-coded. You need to check the corresponding Simulink block documentation for description of the algorithm used. You can find documentation for M-PSK Modulator Baseband at http://www.mathworks.com/help/comm/ref/mpskmodulatorbaseband.html which has more description about the algorithm implemented.