Error using fprintf Invalid file identifier. Use fopen to generate a valid file identifier.
Show older comments
Hi everyone, am getting this error message and i don't know how to fix it.
B = [STR_DATE,num2str(lon),num2str(lat),STR_DUR];
gn=sprintf('events/%s/%s',DUR_NAME,STR_DATE);
fid2 = fopen(gn,'wt');
fprintf(fid2,'%10.2f %10.2f %10.2f %10.2f \r\n',B');
fclose(fid2);
this is how i used the fprintf function.
7 Comments
Walter Roberson
on 12 Jul 2018
Your B is going to turn out as either a character vector or 2D character array if it works. That is unlikely to produce the results you want with the %f format.
What are the class() and size() of the variables you are putting together into B?
You are putting four variables together into B and you have four format items, as if you are expecting each variable to translate into a single column. But for gn to work properly, STR_DATE would pretty much need to be a character row vector, which would be unlikely to match the number of rows in lon and lat, and would be unlikely to be printed with a %f format.
siya lolo
on 12 Jul 2018
Walter Roberson
on 13 Jul 2018
If STR_DATE is a string() array, then when you use
sprintf('events/%s/%s',DUR_NAME,STR_DATE)
it is the same as if you had used
sprintf('events/%s/%s', DUR_NAME, char(strjoin(STR_DATE(:), ''))
so all of the entries in the string array get concatenated together to form the file name. Are you sure that is what is desired?
siya lolo
on 13 Jul 2018
Adam Danz
on 13 Jul 2018
The error "Invalid file identifier" is in reference to the fid2 variable if I'm not mistaken and that indicates the fopen() function didn't successfully access your file. Have you tested my response in the answer section below?
siya lolo
on 13 Jul 2018
Answers (2)
Categories
Find more on Database Toolbox 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!