persistent variable error in code generation
11 views (last 30 days)
Show older comments
Hi everyone,
I encountered a problem when I convert a matlab file into a Mex file. The code generation throws me the following error:
Persistent variable 'default_options' must be assigned before it is used.
The only exception is a check using 'isempty(default_options)'
that can be performed prior to assignment.
In particular, the Matlab code I am converting is one of the functions in the MatlabBGL package: please find the source code below (I cleared some comments in the source code for simplification). This package is useful to find the largest connected set in a (large) binary matrix.
function old_default = set_matlab_bgl_default(varargin)
% SET_MATLAB_BGL_DEFAULT Sets a default option for the Matlab BGL interface
% Example:
% % tranpose the matrix initially...
% At = A'
% old_options = set_matlab_bgl_default(struct('istrans',1));
% % perform a bunch of graph work with At...
% d1 = dfs(At,1); d2 = dfs(At,2); ...
% % restore the old options
% set_matlab_bgl_default(old_options);
% David Gleich
% Copyright, Stanford University, 2006-2008
persistent default_options;
if ~isa(default_options,'struct')
% initial default options
default_options = struct('istrans', 0, 'nocheck', 0, 'full2sparse', 0);
end
if nargin == 0
old_default = default_options;
else
old_default = default_options;
default_options = merge_options(default_options,varargin{:});
end
Since I have never used "persistent" variables before, I am not sure how I could fix this error so that I can proceed to code generation in Matlab coder.
One alternative I tried is to use the official graphic functions (graph and conncomp), but neither of them supports the code generation.
Thanks, and I look forward to hearing from you!
Best,
Long
1 Comment
Ji Lee
on 28 Mar 2022
Edited: Ji Lee
on 28 Mar 2022
The error is stating that MATLAB Coder is expecting the generic idiom "isempty(default_options)" and not the more specific "~isa(default_options, 'struct')" that the code is currently using. Thus, modifying that "if" condition should eliminate that particular error.
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!