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

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

 採用された回答

dpb
dpb 2015 年 8 月 10 日
編集済み: dpb 2015 年 8 月 11 日
...
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 件のコメント

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.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 8 月 10 日
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 件のコメント

Thank you walter Roberson. Removing fclose from within the while loop also helped. Thanks once again. Sloppy programming from my end.
David Pereira
David Pereira 2017 年 3 月 14 日
編集済み: dpb 2017 年 3 月 14 日
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.
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
Joko Widodo 2017 年 12 月 6 日
編集済み: Walter Roberson 2017 年 12 月 6 日
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?
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

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

カテゴリ

ヘルプ センター および File Exchange で MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by