function [txt, s, x] = myApp(n, nslots)
% function [txt, s, x] = myApp(n, nslots)
% Purpose: Demonstrates running app via MATLAB's batch cmd
%          Output x resides on workers, must be gathered 
%          in order to be returned thru "batch" to 
% Computes s = 1 + 2 + 3 + . . + n with nslots cores

% Compute with the Parallel Computing Toolbox

x = distributed.zeros(1,nslots);
spmd
  x(labindex) = labindex;
end
x = gather(x); % req'd if MATLAB's "batch" used

s = 0;    % initialize sum of arithmetic sequence
parfor i=1:n
    s = s + i;       % s = 1 + 2 + 3 + . . + n
end

txt=sprintf(['Compute s = 1 + 2 + 3 + . . + n (n = %d)\n' ...
  'with Parallel Computing Toolbox parfor (%d cores)\n' ...
  'Computed s = %d; Expected s =  %d\n'], ... 
  n, nslots, s, n*(n+1)/2);

end   % end of function 

