Skip to content

Instantly share code, notes, and snippets.

@muddana
Created September 16, 2011 16:16
Show Gist options
  • Save muddana/1222462 to your computer and use it in GitHub Desktop.
Save muddana/1222462 to your computer and use it in GitHub Desktop.
An attempt at Ruby type conditional expression support in MATLAB
%/
% Developer : Srinivas Muddana (srinivas.muddana@gmail.com)
% All code (c)2012 Srinivas Muddana. all rights reserved
%
% do a = a*-1 if(a<0);
% can add a ternary operator too.??
function do(varargin)
%expr = size(2*length(varargin) -1) ;
expr = '';
for(ii = 1:length(varargin))
expr = [expr ' ' varargin{ii}];
end
[a,b, c, d] = regexp(expr, '(.*)if(.*)');
bodyIndexRange = c{1}(1,:);
ifBodyStr = expr(bodyIndexRange(1):bodyIndexRange(2));
condIndexRange = c{1}(2,:);
ifCondStr = expr(condIndexRange(1):condIndexRange(2));
code = ['if(' ifCondStr ')' char(10) ...
ifBodyStr ';' char(10)...
'end' char(10)];
evalin('caller', code);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment