(matlab) how to load adaboost model so that coder compatible? - matlab

I save my adaboot model as .mat file. I use this to load the model:
load('adaboost_23.mat')
But matlab coder cannot generate C/C++ code. So I change to:
coder.load('adaboost_23.mat')
Still not working:
How should I do it? Data type is ClassificationEnsemble.

Now I know. According to matlab guidance, you have to compact model first.
mdl = load('train_model.mat');
saveLearnerForCoder(mdl, 'compacted_model');
And load model.
cpt = loadLearnerForCoder('compacted_model');
After that, Coder will hard code your model and you will get your model in cpp file.

Related

Using Weka NaiveBayes with Matlab

I created a NaiveBayes model in Weka. I exported the model to disk. I now want to inject this model into MATLAB 2018, so that I can check how it performs via some data that I am receiving.
I load my model in MATLAB, by stating something like this:
loadedModel = weka.core.SerializationHelper.read('myweka.model');
I then create a Weka Instance object, and let it contain this data:
instance = infrequent,low,high,medium-high,high,medium,medium,low,low
If I run these two commands:
loadedModel.distributionForInstance(instance)
loadedModel.classifyInstance(instance)
I see the following output:
0.0001
0.9999
1
This is odd to me because if I observe the same record in WEKA ui, I see the same instance with probabilities 0.993 and 0.007, classified as '2'. (I can load the same model multiple times from disk in WEKA, and reproduce this behavior, which is correct) After further investigation, I noticed that regardless of the sequence of attributes my Instance object has, I always get the same probability output and the same classification by invoking the model via MATLAB.
There are some posts on the net that share the same problem, like these:
Always getting the same output
Weka - Classifier returns the same distribution for any input
However, the recommended solution to call 'instance.setClassMissing()' did not solve my issue. Is there anything I am missing, or can try to do in order to further troubleshoot the issue?
Does your test instance has same structure as your train set? If not, you need yo provide the same structure.
Weka indexes nominal attributes and stores the indices internally. So the nominal attributes order in train file is important. For example if your attribute is mapped as low=>0, high=>1 in training, you need to map them like this in your test set. Usually this is achieved by serializing the train header with the model.
Sample code for creating train header:
Instances trainHeader = new Instances(instances, 0);
trainHeader.setClassIndex(instances.classIndex());
When creating a new instance set its dataset:
Instance instance = ...
instance.setDataset(trainHeader);

MATLAB/Embedded Coder File Loading

I generated code for loading a mat file as follows
data=coder.load('data.mat');
a=data.a;
b=data.b;
Because one of the variables, for example "a", is very big, it is defined as a big static const array in the main function with all values initialized there.
Is there any way I can make MATLAB Coder load data from a file in C Code instead of defining it as a variable in main function?
The MATLAB fread function is supported for code generation. So you can fwrite the data to a file in MATLAB and then fread it in the generated code. This will do a runtime read and avoid the giant constant in the generated code.
This is the exact code that we should use based on Ryan's answer:
load('Data.mat')
fileID = fopen('Data.bin', 'w');
fwrite(fileID, Matrix1,'uint64');
fclose(fileID);
fileID=fopen('Data.bin');
Matrix2=fread(fileID,[256,256],'uint64');
fclose(fileID);
Matrix 2 is now the same as Matrix 1. The trick for writing and reading is to use the same precision based on the data type.

Interfacing Simulink with MATLAB

Designing a model which is doing some comparison on data ,fetched from GUI.
I have ".m file" which has GUI functionality based on GUI GUIDE.I want to run my .m file (Inside I simulate my model also once data has been read.) I am using one Push Button on GUI.After pushing that button my model starts simulating as per code mentioned below.
h=str2num(get(handles.edit_h,'String'));
l=str2num(get(handles.edit_l,'String'));
options = simset('SrcWorkspace','current');
sim('level_monitor',[],options);
My model gives output as constant values(like 1,2,3,4).I dont want to plot graph on scope but want to use this constant variables in GUI for setting string message on UI.How to access value coming to output port of simulink via GUI function(m-script).
As per my understanding MATLAB code uses its own workspace and Simulink has its own workspace.(Base and model workspace). How to read data available at Simulink outport to my matlab code(GUI .m file)? I have tried using "Simout(To Workspace block) also but it not resolve my issue.
Kindly help me out with this.
You should be using the form of the sim function that returns an output structure, i.e.
simOut = sim('level_monitor',[],options);
simOut is then a structure that contains fields for all of the variables that the simulation would nominally write to the Base Workspace.
See
>> doc sim
for more information.

Matlab: How can I store the output of "fitcecoc" in a database

In Matlab help section, there's a very helpful example to solve classification problems under "Digit Classification Using HOG Features". You can easily execute the full script by clikcing on 'Open this example'. However, I'm wondering if there's a way to store the output of "fitcecoc" in a database so you don't have to keep training and classifying each and everytime you run the code. Here is the section of the code that's relevant to my question:
% fitcecoc uses SVM learners and a 'One-vs-One' encoding scheme.
classifier = fitcecoc(trainingFeatures, trainingLabels);
So, all I want to do is store 'classifier' in a database and retrieve it for the following code:
predictedLabels = predict(classifier, testFeatures);
Look at Database Toolbox in Matlab.
You could just save the classifier variable in a file:
save('classifier.mat','classifier')
And then load it before executing predict:
load('classifier.mat')
predictedLabels = predict(classifier, testFeatures);

Weka from Matlab : Attribute Selection

I added the Weka.jar file to my environment variables and can already loadARFF files into Matlab and read the instances. Now I would like to perform batch attribute selection on a set of training and testing pairs of files but I cannot seem to find any tutorials on how to do that from Matlab.
I do not want to do it through weka command line because I have a set of 15 training files, and 15 test files for only one trial (and I have many trials with different ARFF files) hence I wanted to loop through them fast from Matlab.
Your help will be highly appreciated :) thanks!
Two things:
First, if you are in the MATLAB Desktop (the MATLAB console) and you preface your command with an exclamation point (!) then you can run command-line arguments. This works in scripts (.m MATLAB files) too.
Ex:
>> !man ls
What this means is that the things you can do in a terminal (like from this tutorial), you should be able to do in MATLAB.
Second, you can access Java libraries from MATLAB. You can access functions from weka.attributeSelection by importing it into your MATLAB workspace and then using the methods you need as you would do in java. For example, here's a .m file written by Matthew Dunham that imports a weka library (weka.core.converters.ArffLoader) and uses it in a .m file:
function wekaOBJ = loadARFF(filename)
% Load data from a weka .arff file into a java weka Instances object for
% use by weka classes. This can be converted for use in matlab by passing
% wekaOBJ to the weka2matlab function.
%
% Written by Matthew Dunham
if(~wekaPathCheck),wekaOBJ = []; return,end
import weka.core.converters.ArffLoader;
import java.io.File;
loader = ArffLoader();
loader.setFile(File(filename));
wekaOBJ = loader.getDataSet();
wekaOBJ.setClassIndex(wekaOBJ.numAttributes -1);
end