matlab fprintf error? please help

1 回表示 (過去 30 日間)
Sojung Youn
Sojung Youn 2020 年 9 月 28 日
コメント済み: Walter Roberson 2020 年 9 月 29 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAudio and Video Data についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by