Error using fprintf. Invalid format
32 次查看(过去 30 天)
显示 更早的评论
This is my code for a function that takes two files and combines parts of these files into one new file.
function []=CreateNewMarkersEEGlab(pNumber)
dataFileName=strcat(int2str(pNumber),'_logfile.txt');
fid = fopen(dataFileName);
C = textscan(fid, '%s%s%s%s%s%s%s%s%s%s%s%s%s%s', 'headerlines', 1);
rScore=C{12};
sNumber=C{5};
cNumber=C{6};
subNumber=C{7};
tcode=C{10};
fclose(fid);
% compute the new marker
for i=1:156
if rScore{i}=='1'
respMarker='corrresp';
else
respMarker='incorrresp';
end
if tcode{i}=='1'
corrMarker='corr';
else
corrMarker='incorr';
end
newMarker(i)= strcat('S1_con',cNumber(i),'_sub',subNumber(i),'_',corrMarker,'_',respMarker,'_SNr',sNumber(i));
end
% read the old marker file
dataFileName=strcat('EEG_Anne_',int2str(pNumber),'.vmrk');
fid = fopen(dataFileName);
headline1=fgets(fid);
headline2=fgets(fid);
headline3=fgets(fid);
headline4=fgets(fid);
headline5=fgets(fid);
headline6=fgets(fid);
headline7=fgets(fid);
headline8=fgets(fid);
headline9=fgets(fid);
headline10=fgets(fid);
headline11=fgets(fid);
headline12=fgets(fid);
headline13=fgets(fid);
C = textscan(fid, '%s%s%d%d%d','Delimiter',',');
Type=C{1};
Position=C{3};
Length=C{4};
Channel=C{5};
fclose(fid);
% rewrite the new marker file
outFileName=strcat('EEG_Anne_',int2str(pNumber),'_new.vmrk');
fid = fopen(outFileName,'w+');
fprintf(fid,headline1);
fprintf(fid,headline2);
fprintf(fid,headline3);
fprintf(fid,headline4);
fprintf(fid,headline5);
fprintf(fid,headline6);
fprintf(fid,headline7);
fprintf(fid,headline8);
fprintf(fid,headline9);
fprintf(fid,headline10);
fprintf(fid,headline11);
fprintf(fid,headline12);
fprintf(fid,headline13);
for i=1:156
temp=[Type(i),',', newMarker(i),',', num2str(Position(i)),',',num2str(Length(i)),',',Channel(i),'\r\n'];
fprintf(fid,temp);
end
fclose(fid);
The error code I get refers to the second last line and reads this:
Error using fprintf Invalid format.
Error in CreateNewMarkersEEGlab (line 73) fprintf(fid,temp);
What am I doing wrong? Thanks a lot for your help!
0 个评论
采纳的回答
Walter Roberson
2015-10-9
The temp you construct is a cell array, not a string. You cannot pass a cell array as a format.
for i=1:156
fprintf(fid, '%s,%s,%d,%d,%s\r\n', Type{i}, newMarker{i}, Position(i), Length(i), Channel{i});
end
4 个评论
Walter Roberson
2015-10-9
for i=1:156
fprintf(fid, '%s,%s,%d,%d,%d\r\n', Type{i}, newMarker{i}, Position(i), Length(i), Channel(i));
end
Note: I recommend that you change how you store newMarker. You currently have
newMarker(i)= strcat('S1_con',cNumber(i),'_sub',subNumber(i),'_',corrMarker,'_',respMarker,'_SNr',sNumber(i));
and I suggest you change that to
newMarker{i} = sprintf('S1_con%s_sub%s_%s_%s_Snr_%s',
cNumber{i}, subNumber{i}, corrMarker, respMarker, sNumber{i});
Also, the two places you use == '1' should be changed:
if rScore{i}=='1'
to
if strcmp(rScore{i}, '1')
and likewise a few lines down.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Live Scripts and Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!