Error using fprintf. Invalid format

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!

 採用された回答

Walter Roberson
Walter Roberson 2015 年 10 月 9 日

0 投票

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 件のコメント

Anne Mickan
Anne Mickan 2015 年 10 月 9 日
Hi Walter, thanks for your help. If I replace my old code with your new one, however, I get a new error message:
Cell contents reference from a non-cell array object. What should I do?
Error in CreateNewMarkersEEGlab (line 72)
fprintf(fid, '%s,%s,%s,%s,%s\r\n', Type{i}, newMarker{i}, Position(i), Length(i), Channel{i});
Stephen23
Stephen23 2015 年 10 月 9 日
編集済み: Stephen23 2015 年 10 月 9 日
Using the debugger stop your code just before that fprintf and check the class of the variables used in the fprintf statement (i.e. Type, newMarker, Position, and Length). I.e. which are cell arrays, and which are numeric arrays. The simply use {} for the cell array variables, and () for the numeric array variables.
The error messge tis telling you that you are using cell array indexing notation {} with something that is not a cell array. So this is what you need to check for.
I suspect that newMarker might use the wrong kind of brackets, as it is not clear in your code what class of variable it is.
Note that you should avoid using names that differ only in case from inbuilt function names, e.g. Length is very similar to length.
Walter Roberson
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.
Anne Mickan
Anne Mickan 2015 年 10 月 12 日
Thank you very much! That worked!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by