As suggested in the documentation for fitlm
you can use the anova
function on your model. Then extract the values (these will be for all x values) and save by whatever method you prefer:
tbl = anova(lm);
% something like this for just your desired values
A = [double(tbl.F),double(tbl.pValue)];
csvwrite('output.csv',A);
% or this dumps the entire result of anova to file
tbl2 = dataset2table(tbl);
writetable(tbl2, 'output.csv');
Okay for the other variant - if you want to use multiple inputs of X then you can use the summary
option on anova
and extract the F and p values from that:
X = cell array of inputs of length n;
F = zeros(n,1);
p = zeros(n,1);
for m = 1:n;
lm = fitlm(X{n},y,'linear')
tbl = anova(lm,'summary');
% you may want to check these indices but should be the right points:
F(n) = double(tbl(2,4));
p(n) = double(tbl(2,5));
end