Error using fprintf Invalid file identifier. Use fopen to generate a valid file indetifier

2 views (last 30 days)
Hello all,
I'm getting the following error message when using fprintf to print a concatenated string to the console output:
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in print_Test2_Stats (line 11)
s2 = "TOTAL FRAMES SENT = " + Total_Frames_t2_64Mb + "\n"; fprintf(s2);
Here is the extract from my code:
fprintf('\n');
fprintf('======================= Test 2 Round Robin Summary ======================\n');
fprintf('BOND TYPE = HOMOGENEOUS 11ac\n');
fprintf('TEST TYPE = LHS (n = 1)\n');
fprintf('SCHEDULING = ROUND ROBIN\n');
fprintf('SELECTION = STATIC\n');
fprintf('HOLD VALUE = N/A\n');
s1 = "TOTAL TIME TAKEN (us) = " + Total_Time_t2_64Mb + "\n"; fprintf(s1);
s2 = "TOTAL FRAMES SENT = " + Total_Frames_t2_64Mb + "\n"; fprintf(s2);
s3 = "TOTAL BITS SENT = " + Total_Bits_t2_64Mb + "\n"; fprintf(s3);
s4 = "TOTAL BYTES SENT = " + Total_Bytes_t2_64Mb + "\n"; fprintf(s4);
s5 = "TOTAL KILOBYTES SENT = " + Total_KBytes_t2_64Mb + "\n"; fprintf(s5);
s6 = "TOTAL MEGABYTES SENT = " + Total_MBytes_t2_64Mb + "\n"; fprintf(s6);
s7 = "MEAN CHAN THROUGHPUT = " + Ch_Bond_Tput_Mean_t2_64Mb + "\n"; fprintf(s7);
I have another similar function called print_Test1_Stats with exactly the same code and this is working fine:
fprintf('\n');
fprintf('======================= Test 1 Round Robin Summary ======================\n');
fprintf('BOND TYPE = HETEROGENEOUS 11ac-11ah\n');
fprintf('TEST TYPE = BALANCED\n');
fprintf('SCHEDULING = ROUND ROBIN\n');
fprintf('SELECTION = STATIC\n');
fprintf('HOLD VALUE = N/A\n');
s1 = "TOTAL TIME TAKEN (us) = " + Total_Time_t1_64Mb + "\n"; fprintf(s1);
s2 = "TOTAL FRAMES SENT = " + Total_Frames_t1_64Mb + "\n"; fprintf(s2);
s3 = "TOTAL BITS SENT = " + Total_Bits_t1_64Mb + "\n"; fprintf(s3);
s4 = "TOTAL BYTES SENT = " + Total_Bytes_t1_64Mb + "\n"; fprintf(s4);
s5 = "TOTAL KILOBYTES SENT = " + Total_KBytes_t1_64Mb + "\n"; fprintf(s5);
s6 = "TOTAL MEGABYTES SENT = " + Total_MBytes_t1_64Mb + "\n"; fprintf(s6);
s7 = "MEAN CHAN THROUGHPUT = " + Ch_Bond_Tput_Mean_t1_64Mb + "\n"; fprintf(s7);
This error started appearing out of the blue and has worked fine before. Could this be a bug or is it a programming fault?

Accepted Answer

Adam Danz
Adam Danz on 30 Jul 2020
Edited: Adam Danz on 30 Jul 2020
My guess is that Total_Time_t2_64Mb is equal to NaN which would result in
s2 = "TOTAL FRAMES SENT = " + NaN + "\n"
s2 =
<missing>
which would cause the error,
fprintf(missing)
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
The first input to fprintf is either a file ID or a format string.
When a missing value is provided, Matlab doesn't know how to interpret that and it goes with the first syntax. The fileID cannot be missing.
Even if you specify the fileID and use the missing value as the formatSpec, you'll still get an error.
fprintf(1,missing)
Error using fprintf
Invalid format.
TL;DR
You cannot supply fprintf with missing values. Look into the section of code assigning a value to Total_Frames_t2_64Mb to see what's going on.
  1 Comment
Michael Dilmore
Michael Dilmore on 30 Jul 2020
Turned out I was passing a vector due to a mix up in the returns values in a completely different function. As soon as fprintf was passed the correct string/scalar value, it worked perfectly.
Really appreciate the response thanks again.

Sign in to comment.

More Answers (2)

Michael Dilmore
Michael Dilmore on 30 Jul 2020
Hi Adam, thanks for answering.
It's given as a vector value for some reason:
s2 =
1×100 string array
Columns 1 through 6
"TOTAL FRAMES SENT…" "TOTAL FRAMES SENT…" "TOTAL FRAMES SENT…" "TOTAL FRAMES SENT…" "TOTAL FRAMES SENT…" "TOTAL FRAMES SENT…"
Columns 7 through 12
"TOTAL FRAMES SENT…" "TOTAL FRAMES SENT…" "TOTAL FRAMES SENT…" "TOTAL FRAMES SENT…" "TOTAL FRAMES SENT…" "TOTAL FRAMES SENT…"
Columns 13 through 18
"TOTAL FRAMES SENT…" "TOTAL FRAMES SENT…" "TOTAL FRAMES SENT…" "TOTAL FRAMES SENT…" "TOTAL FRAMES SENT…" "TOTAL FRAMES SENT…"
While in the Test 1 function this is just a single string:
s2 =
"TOTAL FRAMES SENT = 50000\n"

Michael Dilmore
Michael Dilmore on 30 Jul 2020
I have it - it was a programming error - just need to figure out where now. Thanks for your help

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!