Error in Reading and Writing a text file
Mostra commenti meno recenti
I have a problem with my program when i read and write the file. The following code is what I have so far. It works when I use only textscan and fopen with 'r' instead of 'r+'. The error is Invalid file identifier, Use fopen to generate a valid file identifier. Can somebody see a mistake? Thank you in advance
fid = fopen('3.csv', 'r+'); %Opens the file
NData = textscan(fid, '%s %s %d % ... %d', 'delimiter' ,',', 'headerlines',2);
T_DB = NData {32}; %Gets Dry Bullb Temperature
T_DP = NData {35}; %Gets Dew Point Temperature
RH = NData {38}; %Gets Relative Humitidy
Temp_data = [T_DB T_DP RH];
frewind(fid)
fprintf(fid, '%f %f %f', Temp_data);
fclose(fid); %Closes the file
Risposta accettata
Più risposte (1)
Fangjun Jiang
il 11 Lug 2011
1 voto
Since you are reading and writing the same file, it is surprise to hear that 'r' (read only) works but 'r+' (read and write but not to create new) doesn't. Are you sure it is not the opposite?
Since you are read and write text file, I wonder if 'r+t' would help.
5 Commenti
X
il 11 Lug 2011
Fangjun Jiang
il 11 Lug 2011
I wonder if you have the file opened but not closed due to debugging. Can you try fclose('all') and then try again. BTW, it is 'r+t', not 'rt+'.
Walter Roberson
il 11 Lug 2011
BTW, 'rt+' is fine
http://www.mathworks.com/help/techdoc/ref/fopen.html
To open files in text mode, attach the letter 't' to the
permission, such as 'rt' or 'wt+'
Fangjun Jiang
il 11 Lug 2011
All right, good to know! The example provided in help fopen shows 'w+t'
Jan
il 11 Lug 2011
@Jiang: Walter's link contains a 'wt+' example also.
Categorie
Scopri di più su Data Import and Export in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!