MATLAB: How to vectorize writing to a txt file

vectorization file text data output write

Hi there,
I have to write a function in matlab to create a txt file with the following format:
(x(1),y(1)),(x(2),y(2)),…(x(end),y(end))
I have the following piece of code (whre L is the length of vectors x and y)
for r=1:L;fprintf(fileOutput,['(',num2str(time(r)),',',num2str(signal(r)),'), ']);end
which obviously does the trick but I was wondering if anybody knows how to vectorize the expresion so that it runs much faster.
Any help will be welcomed!!
Thank you!

Best Answer

You could go for something like this:
buffer = sprintf('(%f,%f),', [time; signal]) ; % FormatSpec repeated.
buffer(end) = [] ; % Remove extra ','.
fid = fopen('output.txt', 'w') ;
fwrite(fid, buffer) ;
fclose(fid) ;