persistent variable error in code generation

12 次查看(过去 30 天)
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 个评论
Ji Lee
Ji Lee 2022-3-28
编辑:Ji Lee 2022-3-28
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Coder 的更多信息

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by