Error using fwrite Invalid file identifier. Use fopen to generate a valid file identifier- eventhough fid is positive

4 views (last 30 days)
Hello all, I am trying to edit a base txt file by trying to locate a particular string and then replacin gthe text inder that heading (match1 match2 etc). Even though my file identifier is positive fwrite command is creating a problem. I am not quite sure as to how to proceed from here. Please find below my code and feel free to give suggestions.
cd ('filelocation')
format long g
z=1;
current=dlmread('current.txt')
[Length Width]=size(current)
% open file for reading
fidr= fopen('Base.yml','r');
for k = 1:Width-1
% open file for writing
filename=['Base_',num2str(k),'.yml'];
fidw= fopen(filename,'w')
for i = 1:Length
fidw= fopen(filename,'w')
% RUN until the end of file
while(~feof(fidr))
% get file identifier for next line
str=fgets(fidr);
% match the string in the bracket
match = regexp(str,'- Name: Current1','match');
% match the string in the bracket
match1= regexp(str,'CurrentDepth, CurrentFactor, CurrentRotation:', ...
'match');
% match the string in the bracket
match2= regexp(str,'ActiveCurrent: Current1','match');
if(~isempty(match))
str=[' ' '- Name: Current2',char(10) ];
elseif(~isempty(match2))
str=[' ' 'ActiveCurrent: Current2',char(10) ];
end
% this is where my error gets thrown up
fwrite(fidw,str);
if(~isempty(match1))
for i=1:Length
fprintf(fidw,'%s%1.0f%s%1.5f%s%1.0f%s\n', ...
' - [',current(i,1),', ',current(i,k+1),', ',0,']');
end
fgets(fidr)
fgets(fidr)
fclose(fidw);
end
end
end
end

Accepted Answer

dpb
dpb on 10 Aug 2015
Edited: dpb on 11 Aug 2015
...
filename=['Base_',num2str(k),'.yml'];
fidw= fopen(filename,'w')
for i = 1:Length
fidw= fopen(filename,'w')
...
The file is already open...and you've overwritten the file handle with the subsequent call so you've orphaned the original one.
Not sure what your intent is, but superficially looks as though you simply need to remove the fopen and the fclose inside the loop. Since you don't ever change filename that'll write everything to one file; otherwise you would need to keep them inside the loop but remove the open outside and also do something to create another filename each pass.
  1 Comment
Vikram Suriyan
Vikram Suriyan on 11 Aug 2015
Thank you dpb, This was part of the error and also the one highlighted by the gentleman named walter Roberson below. Sloppy programming from my end.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 10 Aug 2015
You fopen() the file before the "for i" loop and also in the "for i" loop. That is going to lead to problems.
You fclose() if ~isempty(match1) which is within the "while(~feof(fidr))". So you get a match once, close the file, then continue up into the next iteration of the "while" and go ahead and fwrite(fidw) there without having opened fidw after having closed it.
Your ~isempty(match1) code is within "for i" but inside that case you have another "for i" loop. Re-using the loop index is asking for trouble.
  5 Comments
Joko Widodo
Joko Widodo on 6 Dec 2017
Edited: Walter Roberson on 6 Dec 2017
I also have same problem when running my program, as follow:
--------------------------------
clear all
clc
row=23210;
col=7416;
%%Open binary file
fid=fopen('D:\ResearchTemp\0000138094_001001_ALOS2153270000-170325-cal\s11.bin','r');
s11=fread(fid,[2*col row], 'float32');
fclose(fid);
s11_r=s11(1:2:end,:);
s11_i=s11(2:2:end,:);
s11_c=complex(s11_r,s11_i);
M11 = abs(s11_c);
M11 = M11.^2;
M11 = 10*log(M11)-51;
%%Save as new file use w+
fid=fopen('D:\Research 2017\0000138094_001001_ALOS2153270000-170325\SM011.bin','w+');
fwrite(fid,M11, 'float32');
fclose(fid);
---------------------------
Could you give me some opinion?
Walter Roberson
Walter Roberson on 6 Dec 2017
outfile = 'D:\Research 2017\0000138094_001001_ALOS2153270000-170325\SM011.bin';
[fid, message] = fopen(outfile,'w+');
if fid < 0
error('Failed to open file "%s" because "%s"', outfile, message);
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!