Error using fwrite Invalid file identifier. Use fopen to generate a valid file identifier- eventhough fid is positive
Show older comments
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
More Answers (1)
Walter Roberson
on 10 Aug 2015
0 votes
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
Vikram Suriyan
on 11 Aug 2015
David Pereira
on 14 Mar 2017
Edited: dpb
on 14 Mar 2017
Hello,
I am having the same problem.
My program is the following:
% Find a serial port object.
obj1 = instrfind('Type', 'serial', 'Port', 'COM3', 'Tag', '');
obj1.Baudrate=115200;
obj1.Timeout=2000;
obj1.Terminator='CR';
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = serial('COM3');
else
fclose(obj1);
obj1 = obj1(1)
end
% Connect to instrument object, obj1.
fopen(obj1);
%send message
fprintf(obj1,'#1Zmt\r');
%read the command
fscanf(obj1)
Could you help me?
Thank you.
The error is the same:Error using fclose Invalid file identifier. Use fopen to generate a valid file identifier.
Walter Roberson
on 14 Mar 2017
Your fopen() is failing for some reason, and you are not checking for that. fopen() of a serial port does not return a file identifier so you cannot check for negative like you can with a file, but you can check the object Status property; https://www.mathworks.com/help/matlab/matlab_external/status.html
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
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
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!