Skip to content
This repository was archived by the owner on Nov 25, 2018. It is now read-only.

Commit 17fcb16

Browse files
committed
Add class property conversion for <R2014b
Starting in R2016a, the documented method to declare class property types is to specify them after the property declaration with a space in between (e.g. HeaderLength uint16). This is different from the previous (undocumented) approach of separating them with the @ symbol (e.g. HeaderLength@uint16). While the old undocumented approach continues to function in R2016a, the documented R2016a convention is not backwards compatible. For m-code loaded in as an array of cells (<R2014b), an algorithm has been added to locate the property definition blocks and run a regex to convert all type specifications to the legacy syntax. See #6, #7
1 parent f986a72 commit 17fcb16

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

mlapp2classdef.m

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ function processapp(pathname, filename, appname, uielementflag)
173173
replace = regexdict.(functionstoswap{ii}).replace;
174174
mymcode = regexprep(mymcode, expression, replace);
175175
end
176+
177+
% Convert property declarations to backwards-compatible format
178+
mymcode = fixpropertydef(mymcode, isolderthanR2014b);
176179
end
177180

178181
writemfile(mymcode, pathname, appname, isolderthanR2014b);
@@ -222,6 +225,50 @@ function processapp(pathname, filename, appname, uielementflag)
222225
end
223226

224227

228+
function [mymcode] = fixpropertydef(mymcode, oldversionflag)
229+
% Convert property type specifications to backwards compatible format
230+
%
231+
% Starting in R2016a, the documented method to declare class property types
232+
% is to specify them after the property declaration with a space in between
233+
% (e.g. HeaderLength uint16). This is different from the previous
234+
% (undocumented) approach of separating them with the @ symbol (e.g.
235+
% HeaderLength@uint16). While the old undocumented approach continues to
236+
% function in R2016a, the documented R2016a convention is not backwards
237+
% compatible.
238+
%
239+
% See: http://undocumentedmatlab.com/blog/setting-class-property-types-2
240+
% for additional information
241+
242+
if oldversionflag
243+
% MATLAB versions older than R2014b
244+
% Find where the property blocks start and end
245+
propblockstart = find(~cellfun('isempty', regexp(mymcode, '^\s*properties', 'start')));
246+
endstatements = find(~cellfun('isempty', regexp(mymcode, '^\s*end', 'start')));
247+
if ~isempty(propblockstart)
248+
% We have at least one property block
249+
% Pair property block(s) with their end statement(s). Assumes that
250+
% the end statement following each property closes the property
251+
% block, so any logic control inside the property block will be
252+
% broken
253+
npropblocks = length(propblockstart);
254+
propertyblockpair = zeros(npropblocks, 2);
255+
for ii = 1:npropblocks
256+
propertyblockpair(ii, 1) = propblockstart(ii);
257+
% Find first end statement after the property block declaration
258+
propertyblockpair(ii, 2) = endstatements(find(endstatements > propblockstart(ii), 1));
259+
260+
% Go through the block and swap the class property type syntax
261+
tmpblock = mymcode(propertyblockpair(ii,1) + 1:propertyblockpair(ii,2) - 1);
262+
tmpblock = regexprep(tmpblock, '^(\s*\w+)(\s+)(?![\%])', '$1\@');
263+
mymcode(propertyblockpair(ii,1) + 1:propertyblockpair(ii,2) - 1) = tmpblock;
264+
end
265+
end
266+
else
267+
% MATLAB versions R2014b and newer
268+
end
269+
end
270+
271+
225272
function writemfile(mymcode, pathname, appname, oldversionflag)
226273
% Write our m code to a file. Based on the MATLAB version used this will
227274
% either be in a cell array of strings (<R2014b) or a character array

0 commit comments

Comments
 (0)