matlab fprintf error? please help

5 views (last 30 days)
Sojung Youn
Sojung Youn on 28 Sep 2020
Commented: Walter Roberson on 29 Sep 2020
Hi all,
I am very very new to matlab, and I am trying to run a script that will read my txt files and write it on my xlsx file. I have about 120 txt files, so I am trying to run a script on them, but I keep having this fprintf error on a part of my script.
fid = fopen(sprintf('%s/Timeseries.ROIs/%s_timeseries.xlsx',rootdir,rois{roi}),'w');
colhdrs = {'Subject','Group','Gender','Subject','Condition','-4.4', '-2.2', '0', '2.2', '4.4', '6.6', '8.8', '11', '12.2'};
fprintf(fid,'%s\t',colhdrs{:}); fprintf(fid,'\n');
I keep facing an error with the script line below.
frpintf(fid, '%s \ t', colhdrs {:});
saying that it has Invalid file identifier. Use fopen to generate a valid file identifier.
Again, I am very new so any help would be much appreciated. Thank you so much!

Answers (1)

per isakson
per isakson on 29 Sep 2020
Most likely, fid = fopen(...), failed and return a negative value to fid.
Replace
fid = fopen(sprintf('%s/Timeseries.ROIs/%s_timeseries.xlsx',rootdir,rois{roi}),'w');
by
ffs = fullfile( rootdir, 'Timeseries.ROIs', [rois{roi},'_timeseries.xlsx'] );
[fid,msg] = fopen( ffs, 'w' );
and run the script and inspect the values of ffs, fid and msg.
  1 Comment
Walter Roberson
Walter Roberson on 29 Sep 2020
And remember that when you fopen() a file for 'w', that the underlying operating system will not automatically create any missing directories. If rootdir is not a directiory, or if Timeseries.ROIs is not a directory within rootdir, then the fopen() would fail.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!