One way would be to use a counter-variable within the if-condition which increases every time the condition is met.
Then this counter can be used to index a storage vector.
function [peak] = findpeaks(t,ECG)
counter = 1;
storageVec = zeros(length(ECG),2)
prompt = 'Threshold: ';
threshold = input(prompt);
for k = 2 : length(ECG) - 1
if (ECG(k) > ECG(k-1) && ECG(k) > ECG(k+1) && ECG(k) > threshold)
disp('prominant peak found')
peak(counter,1) = ECG(k);
peak(counter,2) = t(k);
counter = counter+1;
plot (t, ECG, t(k), ECG(k), '.')
peak = peak(1:counter);
I would highly reccomend, though, to check out MATLAB's built-in findpeaks function, which has a lot of options like minpeakdistance, minpeakprominence, threshold,...
This makes it really easy to look for R-peaks, T-waves,...
Considering using the diff-function, you could use it on the ecg signal to find maxima, but without additional steps such as thresholding, this gives you all maxima, not just the R-peaks.
For user input, see code above.
Just like Guillaume pointed out, for something like a threshold query you don't need a loop, though.
try something like
function [peak, times] = findpeaks(t, ecg, threshold)
peak = ecg(ecg > threshold);
times = t(ecg > threshold);