Thesym function refers to a symbolic variable, which you can then assign to a MATLAB variable with a different name. For example, the command f1 = sym('x') refers to the symbolic variable x and assigns it to the MATLAB variable f1.
Use the syms function to create a symbolic variable x and automatically assign it to a MATLAB variable x. When you assign a number to the MATLAB variable x, the number is represented in double-precision and this assignment overwrites the previous assignment to a symbolic variable. The class of x becomes double.
Use syms to create a symbolic variable that is assigned to a MATLAB variable with the same name. You get a fresh symbolic variable with no assumptions. If you declare a variable using syms, existing assumptions are cleared.
Use sym to refer to an existing symbolic variable. If this symbolic variable was used in your MATLAB session before, then sym refers to it and its current assumption. If it was not used before, then sym creates it with no assumptions.
The command syms a [1 3] creates a 1-by-3 symbolic array a and the symbolic variables a1, a2, and a3 in the workspace. The symbolic variables a1, a2, and a3 are automatically assigned to the symbolic array a.
The command a = sym('a',[1 3]) refers to the symbolic variables a1, a2, and a3, which are assigned to the symbolic array a in the workspace. The elements a1, a2, and a3 are not created in the workspace.
To declare a symbolic variable within a function, use sym. For example, you can explicitly define a MATLAB variable x in the parent function workspace and refer x to a symbolic variable with the same name.
Functions operate on variables within their own workspace, which is also called the local workspace, separate from the workspace you access at the MATLAB command prompt which is called the base workspace.
The first line of a function starts with the keyword function. It gives the name of the function and order of arguments. In our example, the mymax function has five input arguments and one output argument.
An anonymous function is like an inline function in traditional programming languages, defined within a single MATLAB statement. It consists of a single MATLAB expression and any number of input and output arguments.
Any function other than an anonymous function must be defined within a file. Each function file contains a required primary function that appears first and any number of optional sub-functions that comes after the primary function and used by it.
Primary functions can be called from outside of the file that defines them, either from command line or from other functions, but sub-functions cannot be called from command line or other functions, outside the function file.
Let us write a function named quadratic that would calculate the roots of a quadratic equation. The function would take three inputs, the quadratic co-efficient, the linear co-efficient and the constant term. It would return the roots.
A private function is a primary function that is visible only to a limited group of other functions. If you do not want to expose the implementation of a function(s), you can create them as private functions.
The global declaration must occur before the variable is actually used in a function. It is a good practice to use capital letters for the names of global variables to distinguish them from other variables.
deploy(targetObj,functionName) deploys the MATLAB function as a standalone executable on the hardware. When deploying the function, MATLAB generates a deployment report. The report contains the information about the deployment status. Use the report to debug the function and verify that the function is suitable for deployment.
To improve the execution speed of code generated for certain low-level vector and matrix operations (such as matrix multiplication) in a MATLAB Function block, specify that you want the code generator to produce BLAS calls. BLAS is a software library for low-level vector and matrix computations that has several highly optimized machine-specific implementations. The code generator uses the CBLAS C interface to BLAS. If you specify that you want to generate BLAS calls, and the input arrays for the matrix functions meet certain criteria, the code generator produces the BLAS calls. Otherwise, the code generator produces code for the matrix functions.
To produce BLAS calls in generated code, you must have access to a BLAS callback class. A BLAS callback class specifies the BLAS library, the CBLAS header file, certain C data types the particular CBLAS interface uses and the compiler and linker options for the build process.
To indicate that you want to generate BLAS calls and use a specific BLAS library, specify the name of the BLAS callback class. In the Configuration Parameters dialog box, set Custom BLAS library callback to the name of the callback class.
To generate calls to a specific BLAS library in the generated code, write a BLAS callback class. Share the callback class with others who want to use this BLAS library for BLAS calls in standalone code.
The callback class must derive from the abstract class coder.BLASCallback. This example is an implementation of the callback class mklcallback for integration with the Intel MKL BLAS library on a Windows platform.
You must provide the getHeaderFilename, getBLASIntTypeName, and updateBuildInfo methods. The getHeaderFilename method returns the CBLAS header file name. If you are using a different BLAS library, replace mkl_cblas.h with the name of your CBLAS header file. The getBLASIntTypeName method returns the name of the integer data type that your CBLAS interface uses. If you are using a different BLAS library, replace MKL_INT with the name of the integer data type specific to your CBLAS interface. The updateBuildInfo method provides the information required for the build process to link to the BLAS library. Use code that is like the code in the example callback class to specify the location of header file, the full path name of the BLAS library, and the compiler and linker options. If you use the Intel MKL BLAS library, use the link line advisor to see which libraries and compiler options are recommended for your use case.
There are three other methods that are already implemented in coder.BLASCallback. These methods are getBLASDoubleComplexTypeName, getBLASSingleComplexTypeName, and useEnumNameRatherThanTypedef. By default, your callback class inherits these implementations from coder.BLASCallback. In certain situations, you must override these methods with your own definitions when you define your callback class.
The getBLASDoubleComplexTypeName method returns the type used for double-precision complex variables in the generated code. If your BLAS library takes a type other than double* and void* for double-precision complex array arguments, include this method in your callback class definition.
The getBLASSingleComplexTypeName method returns the type used for single-precision complex variables in the generated code. If your BLAS library takes a type other than float* and void* for single-precision complex array arguments, include this method in your callback class definition.
The useEnumNameRatherThanTypedef method returns false by default. If types for enumerations in your BLAS library include the enum keyword, redefine this method to return true in your callback class definition.
This example shows how to generate code that calls BLAS functions in a specific BLAS library. The BLAS callback class useMyBLAS specifies the BLAS library that you want to use in this example.Create a Simulink model.
To specify the rpath linker option, use the build information addLinkFlags method in the updateBuildInfo method of your BLAS callback class. For example, for a GCC compiler:buildInfo.addLinkFlags(sprintf('-Wl,-rpath,"%s"',libPath));
If you generate C++ code that includes calls to OpenBLAS library functions, compiling it with the -pedantic option produces warnings. To disable the -pedantic compiler option, include these lines in the updateBuildInfo method:
Mdl is a ClassificationSVM object, which is a linear SVM model. The predictor coefficients in a linear SVM model provide enough information to predict labels for new observations. Removing the support vectors reduces memory usage in the generated code. Remove the support vectors from the linear SVM model by using the discardSupportVectors function.
Note: If you click the button located in the upper-right section of this page and open this example in MATLAB, then MATLAB opens the example folder. This folder includes the entry-point function file.
The figure displays the Simulink model. When the input node detects a radar return, it directs that observation into the MATLAB Function block that dispatches to svmIonospherePredict.m. After predicting the label and score, the model returns these values to the workspace and displays the values within the model one at a time. When you load slexSVMIonospherePredictExample.slx, MATLAB also loads the data set that it requires called radarReturnInput. However, this example shows how to construct the required data set.
time - The points in time at which the observations enter the model. In the example, the duration includes the integers from 0 though 50. The orientation must correspond to the observations in the predictor data. So, for this example, time must be a column vector.
The figure shows the model after it processes all observations in radarReturnInput one at a time. The predicted label of X(351,:) is 1 and its positive-class score is 1.431. The variables tout, yout, and svmlogsout appear in the workspace. yout and svmlogsout are SimulinkData.Dataset objects containing the predicted labels and scores. For more details, see Data Format for Logged Simulation Data (Simulink).
labelsSL is a 51-by-1 numeric vector of predicted labels. labelsSL(j) = 1 means that the SVM model predicts that radar return j in the future sample is of good quality, and 0 means otherwise. scoresSL is a 51-by-1 numeric vector of positive-class scores, that is, signed distances from the decision boundary. Positive scores correspond to predicted labels of 1, and negative scores correspond to predicted labels of 0.
3a8082e126