MATLAB: Need help the mean of this code

functionsymbol [~]

% GENERATE CODE FOR GENERATING GRID
code_generate_grid = '[';
for i=1:length(grd.Nx)
code_generate_grid = [code_generate_grid 'inp.X{' num2str(i) '} '];
end
for i=1:length(grd.Nu)
code_generate_grid = [code_generate_grid 'inp.U{' num2str(i) '} '];
end
code_generate_grid = [code_generate_grid '] = ndgrid('];
for i=1:length(grd.Nx)
code_generate_grid = [code_generate_grid 'current_grd.X{' num2str(i) '},'];
end
for i=1:length(grd.Nu)
code_generate_grid = [code_generate_grid 'current_grd.U{' num2str(i) '},'];
end
code_generate_grid = code_generate_grid(1:end-1);
code_generate_grid = [code_generate_grid ');'];
can someone explain the meaning from the code with bold text
Any help would be appreciate thank you…..

Best Answer

This is horrible code. I would strongly suggest not using it.
However, it should be easy to see what is happening if you go throught this code line by line with the debugger.
What happens is that it generates a char array
code_generate_grid = '[';%intializes the array
for i=1:length(grd.Nx)
code_generate_grid = [code_generate_grid 'inp.X{' num2str(i) '} '];
%on i=1:
%code_generate_grid='[inp.X{1} '
%on i=2:
%code_generate_grid='[inp.X{1} inp.X{2} '
end
Then that penultimate line crops off the last space and the last line puts in a closing bracket.
DO NOT USE THIS FOR EVAL.