Error using fprintf. Invalid format

44 views (last 30 days)
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!

Accepted Answer

Walter Roberson
Walter Roberson on 9 Oct 2015
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 Comments
Walter Roberson
Walter Roberson on 9 Oct 2015
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.
Anne Mickan
Anne Mickan on 12 Oct 2015
Thank you very much! That worked!

Sign in to comment.

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!