Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 291209

Submitted on 28 Jul 2013 by @bmtran

Correct

52Size
Leading solution size is 34.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
Code Input and Output
1
Pass
 
%%
% Clean workspace
!/bin/cp my_function.m safe
!/bin/rm *.*
!/bin/mv safe my_function.m
!/bin/rm @*
% Clean user's function from some known jailbreaking mechanisms
functions = {'!','feval','eval','str2func','str2num','regex','system','dos','unix','perl','java','fopen','write','save','setenv','path','fft'};
fid = fopen('my_function.m');
  st = char(fread(fid)');
  for n = 1:numel(functions)
    st = regexprep(st, functions{n}, 'error(''No fancy functions!''); %', 'ignorecase');
  end
fclose(fid)
% Force a function header, and comment first code's line (the supposedly users' function header)
st = sprintf('function y = my_function(x)\n%%%s', st);
fid = fopen('my_function.m' , 'w');
  fwrite(fid,st);
fclose(fid)
rng(now);
input = rand(1, 1000);
output = my_function(input);
% just ignore this
!/bin/cp my_function.m safe
!/bin/rm *.*
!/bin/mv safe my_function.m
!/bin/rm @*
path(pathdef); % undo any change on search path
assert(sum(abs(output - fft(input))) < 1e-8) % accounts for some FP errors
/bin/rm: cannot remove `@*': No such file or directory
ans =
     0
ans =
     0
/bin/rm: cannot remove `@*': No such file or directory