Edit: wrote new function StripTildes.m (see below) that works with all cases including output list on multiple lines
Edit: Modified the StripTildes function to use Daniel's regular expression from FEX function. This still does not work with modified TestFunction that uses a line continuation in the output argument list.
My version of Matlab does not support the use of ~ in an output argument list so it is giving syntax errors when I try to use some downloaded code.
My regular expressions are extremely rusty so I would appreciate a regular expression to use with regexprep to replace the ~ in an output argument enclosed in brackets but not replace ~= and other uses of the symbol.
responding to Azzi's comment: I would like to replace ~ with a regular variable name like 'dummy' so it will not cause a syntax error. The rest of the code should then work OK so long as there is no collision with an existing variable name.
My idea is to write a Matlab function to scan the downloaded code file, replace the ~'s, and then write the output to another file.
My editor 'Editpad' allows regular expression search and replace so I can also use the regular expression with it but for definiteness please stick with regexprep and I will take it from there.
Question: I am no clear the allowable syntax. Are only spaces between output arguments allowed such as?
[~ ~ y3 ] = NewFunction(foo);
TIA.
I created a function to implement the filtering and a test function as follows:
1. the filtering function
function StripTildes(filename) % replace tildes in output parameter lists with 'the_dummy'
% in Matlab file filename
% outputs new file named filename_notilde.m to same directory as original file
pattern='~(?=[^\]\[='']*\][\s(\.\.\.)]*=[^=])'; % regular expression to find tildes
fid = fopen(filename); % open for read only
assert( fid ~= -1); % read all file into one long string (including line break chars) so regexprep can work across lines
filetext = []; while 1 tline = fgets(fid); if ~ischar(tline), break, end % quit at end of file
filetext = [filetext tline]; end fclose(fid); filetext = regexprep(filetext,pattern,'the_dummy'); % output the file
[path,name,ext] = fileparts(filename); out_name = [path filesep name '_notilde' ext]; fidout = fopen(out_name,'w'); assert( fid ~= -1); fwrite(fidout,filetext); fclose(fidout);
2. the function to test
function TestFunction if 1 ~= 0 [~,~,y] = LocalFunction; elseif ~(true) or ~exist(foo,'dir') [~ ~ y ] = LocalFunction; end tf = [~true, 1~=0, ... ]; [~,~, ... y] = LocalFunction; function [y1,y2,y3] = LocalFunction y1 = 0; y2 = 0; y3 = 0
p.s. I cannot afford to upgrade my Matlab version so please do not suggest it as a solution
Best Answer