printf - MATLAB - Error while writing output in a CSV file using fprintf -


i consistently getting error while writing output in csv file using fprintf. want write results in csv file. have tried different lengths of matrix, , same error 2 columns. what's mistake here , how can resolve error?

sample code:

colname = {'col1' 'col2' 'col3'}; fid = fopen('test.csv','w'); fprintf(fid, '%s, %s, %s\n', colname{1:}); p=1:5     % <some code>     fname = %reading image name 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. @ fprintf(fid, '%s, %f, %f\n', datacol{p+1:});

p.s.: writing "datacol = {fname val1 val2};" "datacol = {fname,val1,val2};" brought same error message.

you indexing cell contents of datacol. if not mistaken datacol looks sth this:

{'some_string_for_the_name', 1, 2} 

where 1 , 2 val1 , val2. during loop access datacol{p+1} datcol{4} p = 3. since cell has 3 elements, indexing fourth result in error. print lines of val1 , val2, no? changing fprintf

fprintf(fid, '%s, %f, %f\n', datacol{1}, datacol{2}, datacol{3}); 

should solve problem.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -