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

fprintfMATLAB

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?

Best Answer

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.