Help fixing fopen error in code

4 visualizzazioni (ultimi 30 giorni)
Kristine
Kristine il 27 Mag 2025
Risposto: Walter Roberson il 28 Mag 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.

Risposta accettata

Walter Roberson
Walter Roberson il 28 Mag 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

Più risposte (0)

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Tag

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by