You can make fit
a function, instead of script. The fit
function can have a single input variable pointing to the right data file.
Then you can run
matlab -nodisplay -nojvm -nosplash -r "cd ${pathtoscript}; fit('${dataofinterest}');exit;"
EDIT: added this detailed fit
fun.
Your fit
function should look something like
function fit( variableName )
%
% run fit for a specific variable from the mat file
% variableName is a string, e.g. variableName = 'rgb1';
%
ld = load('/pathtomatfile/alldata.mat');
count = ld.(variableName); % access variables in mat file as struct fields
count = double( count );
% and I believe you can take it from here...
EDIT: Similar solution loading mat file into a struct to handle the loaded variables can be found here with some more details.