Help fixing fopen error in code
4 views (last 30 days)
Show older comments
Kristine
on 27 May 2025
Answered: Walter Roberson
on 28 May 2025
MATLAB is not letting me create breaks between text…
Hi y’all,
I am trying to sun the following code:
files=dir('txt*.txt'); %additional filter so i didn't read in my merged file
fileout='merged.txt';
fout=fopen(fileout,'w');
for cntfiles=1:length(files)
fin=fopen(files(cntfiles).name);
temp = fread(fin,'uint8');
fwrite(fout,temp,'uint8');
fprintf(fout,'\n');
fclose(fin);
end
fclose(fout);
When I run it, I get an error:
Error using fread.
Invalid file identifier. Use fopen to generate a valid file identifier.
But I am using fopen. I don’t know how to resolve this issue.
0 Comments
Accepted Answer
Walter Roberson
on 28 May 2025
filename = fullfile(files(cntfiles).folder, files(cntfiles).name);
[fin, msg] = fopen( filename );
if fin < 0
error('failed to open "%s" because "%s"', filename, msg);
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Get Started with 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!