Control character error arising only after compiling script to an executable.

I am using an application that has the user create configuration files through MATLAB scripts. The user then has to encrypt these configuration files into the application's own file format. The encrypter is provided as a group of p-code files and is simply called in the command window with:
encrypt(<filename.m>)
Instead of encrypting each file manually, I wrote a script to loop over all the configuration files and encrypt them. Everything works fine if this script is run from the MATLAB editor. However, if I compile the script using MATLAB Compiler through the "deploytool" command, I get an error which causes the executable to stop running because it can't find all the right p-code files. Below is the error I receive:
Warning: Control Character '\+' is not valid. See 'doc sprintf' for control characters valid in the format string.
I think have a good understanding of what the error means and I believe it would be fixed if the single backslash was replace by a double backslash. The '\+' is referring to the start of the name of a subdirectory located within the p-code encrypter files. This subdirectory contains a p-code function which is required to complete the encryption but obviously this p-code function cannot be called if a previous function cannot cd to this respective directory.
My main question is why this only occurs when trying to run my script as an executable and if there are any methods I can try to remedy this error. I cannot change the encryption process since it is all in p-code so I have had some trouble trying to think of how I can go about this. Any help is appreciated.

6 个评论

Change the code to use fullfile() instead of putting together directory names using sprintf() .
Unfortunately I cannot change any of the code that encrypts the files since it is all p-code. I am hoping that there is a setting I can use while compiling to make the executable work just like the normal script.
For clarification, the p-code is using sprinf() and I have no way of changing that. For some reason, everything works fine if not compiled into an executable. The control character error only comes up when running the executable I made from my script.
Pre-process what you pass in to the function, replacing \ with \\
I use fullfile() to pass in the configuration file paths to the p-code function but the error does not stem from the paths I pass. Rather, the error comes up when the p-code attempts to navigate to a hard-coded relative path. I have no way of changing this action as it is done from within the p-code.
You could deliberately shadow sprintf
varargout = function sprintf(varargin)
if nargin == 0
error('MATLAB:minrhs')
end
if ~ischar(varargin{1}) && ~isstring(varargin{1})
error('MATLAB:badformat_mx')
end
varargin{1} = regexprep(varargin{1}, '\\\+', '\\\\+');
builtin('sprintf', varargin{:});
end
I was able to get a slightly modified form of the code above to work. Thank you for the help.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by