matlab fprintf error? please help

2 次查看(过去 30 天)
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!

回答(1 个)

per isakson
per isakson 2020-9-29
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 个评论
Walter Roberson
Walter Roberson 2020-9-29
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.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by