Help fixing fopen error in code

4 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2025-5-28
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 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by