matlab coder requirements stricter than normal matlab - matlab

Consider the following matlab program:
function results = prog()
opts.x = 1;
if ~isfield(opts, 'y'); opts.y = 1; end
'asdf'
return
I am able to run this program successfully in matlab however when I try to use coder to convert it to C I get the following error:
This structure does not have a field 'y'; new fields cannot be added when structure has been read or used.
I would like to know if there is a way to convert to C using coder (or possible some other tool) that does not use a stricter compiler as seems to be the case with coder as I am using it. I am using matlab version R2019B.
Please note that this is just one of many examples of how coder is using a stricter compiler than normal matlab. I have a fairly large program that I would like to convert to C and I don't want to have to go through each error (there are over 100).

Like Daniel mentioned, optional fields of structs don't exist in C, which is why MATLAB Coder errors on that code.
To make this code work with MATLAB Coder, opts could always have property y, but make it variable-sized and initialized to empty:
function results = prog()
opts.x = 1;
opts.y = [];
coder.varsize('opts.y');
if isempty(opts.y); opts.y = 1; end
'asdf'
end
Or you could create another options variable optsWithY that will have field y, even if opts doesn't:
function results = prog()
opts.x = 1;
optsWithY = opts;
if ~isfield(opts, 'y'); optsWithY.y = 1; end
'asdf'
end
This could even be moved into a helper function and assigned back to opts:
function results = prog()
opts.x = 1;
opts = addOpt(opts, 'y', 1);
'asdf'
end
function newOpts = addOpt(opts, field, defaultValue)
newOpts = opts;
if ~isfield(opts, field)
newOpts.(field) = defaultValue;
end
end
The difference between this and the original code is partial assignment opts.y = ... vs complete assignment opts = ....
Or like Cris mentioned, MATLAB Compiler will be a lot closer to MATLAB (though you won't get C code)

Related

Defining a function with multiple outputs that can't be organised into a matrix

Is there any natural way to define a MATLAB function with multiple outputs that cannot or are inappropriate to "stack" into a matrix? For example, what if I want a function f that returns a 3x3 matrix A and a 4x4 matrix B?
I'm really surprised that this would even be an issue in MATLAB. Because in Python, all we need to do is return A, B which returns a tuple of the two. However it seems that MATLAB doesn't quite support the idea of containers. As a non-elegant workaround, I can use a struct to put the two pieces of data in, and the function goes something like:
function re = f(x)
%f: returns two dimensional-inconsistent matrices A and B
% function body as follows
....
A = ...;
B = ...;
% put data into the struct 're'
re.A = A;
re.B = B;
end
Apart from possible performance issues, this approach looks very unnatural and clumsy. Is there any better approach?
In MATLAB you can return any number of outputs with this syntax:
function [A,B] = f(x)
A = ...;
B = ...;
end
that is an even elegant solution than tuples used in python.
You can even control the behavior with the number of inputs and outputs (nargin and nargout) and discard outputs with a tilde. More information here.
I cannot think of a more elegant syntax.
Usually when having several outputs, one should declare the function as follows:
function [out1, out2, ... , outN] = funcName(in1,...,inM)
...
end
MATLAB also allows you to alter the behavior of your function based on the amount of requested inputs/outputs via the nargin/nargout functions, respectively (you can think of this as a form of overloading).
For example, you can specify as one of the inputs an array indicating which outputs you want the function to give, then populate the varargout cell array accordingly:
function varargout = funcName(in1,...,whichOut)
...
for indO = 1:numel(whichOut)
switch whichOut{indO}
case 'out1'
varargout{indO} = out1;
case 'out2'
... etc
case 'out6'
varargout{indO} = out6;
end
end
then call it using [out6, out1] = funcName(inp, {'out6','out1'});
See also varargin.

Array of objects in generable matlab function

In simulink model I have a matlab function block. Inside the function I would like to create an array of objects in a way it is compatible with code generation.
My question is similar to the one answered here: Construct an array of objects in MATLAB
The problem is "compatible with code generation" part.
When I try to do it with repmatmatlab returns:
Arrays of objects are not supported for code generation.
When I try to do it with array of objects I see:
Recursive calls are not allowed. Function 'dummyClass.dummyClass'
participated in a recursive call.
Please find below the code I run:
embedded matlab function
function y = fcn(u)
%#codegen
x = [1 2 3];
% %% repmat way
% aa = dummyClass(x(1));
% aaArray = repmat(aa,1,3);
%% array of objects
aa = dummyClass(x);
y = u;
class file
classdef dummyClass
properties
value
end
methods
function obj = dummyClass(value)
%% array of objects
if nargin~=0
m = size(value,1);
n = size(value,2);
obj(m,n) = dummyClass;
for i = 1:m
for j = 1:n
obj(a,b).value = value(a,b);
end
end
end
% %% repmat
% obj.value = value;
end
end
end
Uncomment
As of MATLAB R2017a, there's no way to create arrays of objects that is compatible with code generation using MATLAB Coder or Simulink Coder.
As the first error message says, "arrays of objects are not supported for code generation" - it's not a problem with any specific way you're attempting to create them, they're just not supported at all.
MathWorks may introduce this feature in a future version, but it's not there right now.

Matlab R2014a - defining a variable after a function is called

In a matlab script, is it possible to define a variable after the function is called?
(i.e. the script finds the function with the undefined variable, looks for the variable initialization and executes the function afterwards)
I need this because I have a large number of variables and functions and I don't want to order them in a sequential way but in the way that is appropriate for my problem.
E.g.
mean(x);
x = [1, 2, 3];
Thanks
Disclaimer: I think what you want to do is a really bad practice. If you find yourself in the need to do this, your code screams refactoring.
Yes, it is possible. There are two ways.
Using the try-catch statement:
try
x_mean = mean(x)
catch exception
if isequal(exception.identifier, 'MATLAB:UndefinedFunction')
x = [1,2,3];
x_mean = mean(x);
else
rethrow(exception);
end
end
The explanation: if the mean(x) fails, the code will enter in the catch branch. Then it verifies that the error is happening because a missing variable and if that's the case, the variable x is defined and the function re-run. There is a caveat: Matlab uses the same identifier for missing variable and for missing function.
Using the exist function:
if exist('x') == 1 %// the function have several return values
x_mean = mean(x);
else
x = [1, 2, 3];
x_mean = mean(x);
end
The explanation: the exist function will verify if a variable with the name x exists in the workspace. If it does exist, it will return 1 and you can execute your function. If it does not exist, then you can declare it first and then run mean. See documentation here.
My two cents: once again, this is a horrible way to achieve things. By going this way you will end up with pure spaghetti code. It will be a nightmare to debug. It will be a nightmare to maintain. There is a 99% chance that there are better ways to do what you want. It is just a matter of investing time and think a little bit. Even if it is a personal project or a quick script to solve a homework/task, you will learn much more by doing it the right way.
If I understand you question correctly, then yes it can be done by some ugly hacks.
The following class can be used:
classdef AdvancedFunctionHandle
properties(GetAccess = 'private', SetAccess = 'private')
handle
variables
end
methods
function obj = AdvancedFunctionHandle(fct, varargin)
if ~isa(fct,'function_handle')
error('The function must be a function handle!');
end
argins = numel(varargin);
variables = cell(1,argins);
for i = 1:argins
if ~ischar(varargin{i})
error('The variables must be represented by a string variabel name!');
end
if (~isvarname(varargin{i}) )
error('The variables must be a string representing a legal variabel name!');
end
variables{i} = varargin{i};
end
obj.handle = fct;
obj.variables = variables;
end
function val = subsindex(obj)
val = obj.calculate();
end
function val = calculate(obj)
try
vars = cell(1,numel(obj.variables));
for i = 1:numel(obj.variables)
v = evalin('base',obj.variables{i});
if isa(v,'AdvancedFunctionHandle')
vars{i} = v.calculate();
else
vars{i} = v;
end
end
val = obj.handle(vars{:});
catch
val = obj;
end
end
function display(obj)
disp([func2str(obj.handle),'(',strjoin(obj.variables,','),')']);
end
end
end
I made a small test script for it too:
clear
m = AdvancedFunctionHandle(#mean,'x');
s = AdvancedFunctionHandle(#(n)sum(1:n),'m');
m.calculate
x = [1,2,3];
s.calculate
I hope this solves your problem.
Notice that since the class refers to the base workspace, it will not work when run from a function, or if the script it is run from is run from a function.
I don't understand what you are trying to achieve, but if you want to define things after they are used, I think they have to be functions.
Example file:
function myfun()
mean(getX)
function X = getX
X = [1 2 3]
So though it is technically possible to define variables after they are used, you would really not want to do that.

Expected scalar value: MATLAB Coder

I get the following error while using MATLAB coder for generating a c++ code from m file with a call to a c function ompmex.c
C function calls always return scalar values but a non-scalar value is expected here.
My code is:
function [D,gamma] = DSGD2(X,H) %#codegen
[Xr,Xc]=size(X);
[Hr,Hc]=size(H);
D=zeros(Hr,Hc,'double');
D=X(:,11:210);
d1=sqrt(sum(D.*D)); D=D./repmat(d1,Xr,1);
beta=zeros(Xr,Xc,'double');
beta=coder.ceval('ompmex',H,X,H'*H,200);
Can anybody help on this. I am not getting a working answer online
function [D,gamma] = DSGD2(X,H) %#codegen
[Xr,Xc] = size(X);
[Hr,Hc] = size(H);
D = zeros(Hr,Hc,'double');
D = X(:,11:210);
d1 = sqrt(sum(D.*D));
D = D./repmat(d1,Xr,1);
% coder.ceval is tricky. I recommand to use it only if the function is
% created by c-coder too (and not as mex function!)! you need a c executable
% or a c library (generated before you trigger codegen for DSGD2).
% further more, ceval can not handle multiple
% outputs. as a workaround, always use structs for output - and for
% input too!
s = struct('beta',(zeros(Xr,Xc,'double')));
% make it work in matlab too and call ompmex function
if coder.target('MATLAB')
s = ompmex(H,X,H'*H,200);
else
coder.ceval('ompmex_initialize')
s = coder.ceval('ompmex',H,X,H'*H,200);
coder.ceval('ompmex_terminate')
end
% read out stuct value
beta = zeros(Xr,Xc,'double');
beta = s.beta;
gamma = 0; % not given by you, but c-coder needs it. so change to what ever you need
end

Matlab - Manipulating datasets within subclass methods

I am having some difficulty designing a subclass of dataset in Matlab (R2010b). I am experienced programming Matlab, but new to using its OOP features. My constructor seems to be fine, but when I try to build some methods that access data in the dataset, I can't seem to get it to work. Here is an example:
classdef mydataset < dataset
properties
end
methods
function [obj] = mydataset(obs,array)
% obs is N x 1 cell array of strings
% array is N x 2 double array
obj = obj#dataset({array,'Field1','Field2'},'ObsNames',obs)
end
function [val] = computeValue(obj)
col = obj.Field1;
% I get an error above regardless of how I try to access the dataset.
% e.g. col = double(obj(obs,'Field1')) also does not work.
% Some more code using col to determine val
end
end
end
In my method computeValue, I am trying to access the data in the dataset using dataset syntax, i.e. on the command line I could access Field1 using ".". It complains there is no method, property, or field Field1 for class mydataset. If I try the alternate syntax
col = double(obj(:,'Field1'));
it complains about the size of obj, e.g. "Index exceeds matrix dimensions".
I found a workaround using subsref:
methods
function [val] = computeValue(obj)
s.type = '.';
s.subs = 'Field1';
col = subsref(obj,s);
% Some more code using col to determine val
end
end
Although the workaround works, it is not very convenient and largely defeats the purpose of wanting a subclass of dataset. Is there some attribute or something simple I am missing?
Thank you very much.
Eric
Can you post the complete code of your class. I suppose you didn't declare your property "factor" and try to access it.
Your class should look like this :
classdef MyClass
properties
factor;
end
methods
function obj = MyClass(factor)
% The constructor set the property factor
obj.factor = factor;
end
function val = computeValue(obj)
col = obj.factor;
% ...
end
end
end