I am consistently getting an error while writing output in a CSV file using fprintf. I actually want to write my results in a CSV file. I have tried different lengths of the matrix, and I get the same error even with two columns. What's the mistake here and how can I resolve this error?
Sample code:
colname = {'col1' 'col2' 'col3'};
fid = fopen('test.csv','w');
fprintf(fid, '%s, %s, %s\n', colname{1:});
for p=1:5
% <Some code>
fname = %reading image name from a directory
% <Some code>
val1 = %calculating value1
val2 = %calculating value2
datacol = {fname val1 val2};
fprintf(fid, '%s, %f, %f\n', datacol{p+1:});
end
fclose(fid);
Error:
??? Index exceeds matrix dimensions. at fprintf(fid, '%s, %f, %f\n', datacol{p+1:});
P.S.: Writing "datacol = {fname val1 val2};" as "datacol = {fname,val1,val2};" brought the same error message.