Using coder.target to differentiate between Matlab environment and coder
6 views (last 30 days)
Show older comments
Hi,
I'm trying to provide a map functionality to algorithms written in Matlab which are later converted to C code and used in C framework. As any map (I call it container in the code) it has the porpuse to store different data types in key,value pairs to retrieve them later.
Here is the Matlab code which uses coder.ceval() to call the container functions when generating the C code and at the same time I would like to provide the same functionality when the code is executed in the Matlab runtime environment.
function funs = container_helper_functions() %#codegen
funs.container_add_string = @container_add_string;
funs.container_add_double = @put_double;
funs.container_get_double = @get_double;
funs.container_hasKey = @hasKey;
if coder.target('MATLAB')
global container
container = containers.Map;
else
coder.cinclude('container_wrap.h');
end
end
function container_add_string(key, value) %#codegen
if coder.target('MATLAB')
global container
container(key)=value;
else
key2 = [key,0];
value2 = [value,0];
coder.ceval('put_string', coder.rref(key2), coder.rref(value2));
end
end
function put_double(key, value) %#codegen
if coder.target('MATLAB')
global container
container(key)=value;
else
key2 = [key,0];
coder.ceval('put_double', coder.rref(key2), value);
end
end
function result = get_double(key, result) %#codegen
if coder.target('MATLAB')
global container
result = container(key);
else
key2 = [key,0];
result = 0;
result = coder.ceval('get_double', coder.rref(key2));
end
end
This works fine in the Matlab environment:
fncs.put_double('test',2);
out=fncs.get_double('test')
But when I try to generate C code using the coder I get this error complaining about the global variable even though I use coder.target('MATLAB'):
??? GLOBAL declarations may only appear at the top-level.
Error in ==> container_helper_functions Line: 8 Column: 9
Code generation failed: View Error Report
Shouldn't the global variable be ignored when generating C code since it's guarded by the if statement ?
if coder.target('MATLAB')
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!