Problem with writing datestr converted date time to a text file alongside other numeric variables
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Vincent Odongo
le 22 Fév 2023
Déplacé(e) : Steven Lord
le 25 Fév 2023
I read in my data as an array and select key variables I want written into a text file. The code is as shown below. However when I attempt to run the code I get the following error:
Warning: Out of range or non-integer values truncated during conversion to
character.
The problem seems to be in the DateTime variable. If I exclude it from the output, the text file is printed out well.
However, when I test the DateTime output from the command prompt, the results seem ok. Problem emerges when printing to the file. Any help or workaround solution is welcome.
%% Allocate imported array to column variable names
date1 = dataArray{:, 1}; % Date column is in YYYY-MM-DD
time = dataArray{:, 2}; % Time column is in HH:MM
DOY = dataArray{:, 3}; % Julian date
Ts_1_1_1 = dataArray{:, 9};
Ts_2_1_1 = dataArray{:, 10};
Ts_3_1_1 = dataArray{:, 11};
Ta_1_1_1 = dataArray{:, 18};
RH_1_1_1 = dataArray{:, 19};
SWin_1_1_1 = dataArray{:, 22};
TA_1_2_1 = dataArray{:, 33};
RH_1_2_1 = dataArray{:, 34};
%% Clear temporary variables
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
%% Add date to time to have the format 'yyyy-mm-dd HH:MM'
hh=time-datenum(2023,01,01,0,0,0);
DateTime = datestr(datevec(date1) + datevec(hh), 'yyyy-mm-dd HH:MM');
%% |combine the data to output into a single txt file meteo = [DateTime DOY Ta_1_1_1-273.15 TA_1_2_1-273.15 Ts_1_1_1-273.15 Ts_2_1_1-273.15 Ts_3_1_1-273.15 RH_1_1_1 RH_1_2_1 SWin_1_1_1 ];
meteo_file = fopen('\meteo.csv','w+');
fprintf(meteo_file,'%s \r\n','Datetime DOY Ta1 Ta2 Ts1 Ts2 Ts3 RH1 RH2 SWin');
for i = 1: length (date1)
meteo = [DateTime(i,:) DOY(i,:) Ta_1_1_1(i,:)-273.15 TA_1_2_1(i,:)-273.15 Ts_1_1_1(i,:)-273.15 Ts_2_1_1(i,:)-273.15 Ts_3_1_1(i,:)-273.15 RH_1_1_1(i,:) RH_1_2_1(i,:) SWin_1_1_1(i) ];
fprintf(meteo_file, '%s %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f %6.0f\r', meteo);
end
fclose all;
1 commentaire
Adam Danz
le 22 Fév 2023
There are too many unkowns to easily isolate the problem.
Please provide the entire error message that indicates the line causing the error.
Please provide or describe the data in the variables so we can run the code or understand the size/shape/class of your variables.
Réponse acceptée
Stephen23
le 22 Fév 2023
Modifié(e) : Stephen23
le 22 Fév 2023
Have a look at this line:
meteo = [DateTime(i,:) DOY(i,:) Ta_1_1_1(i,:)-273.15 TA_1_2_1(i,:)-273.15 Ts_1_1_1(i,:)-273.15 Ts_2_1_1(i,:)-273.15 Ts_3_1_1(i,:)-273.15 RH_1_1_1(i,:) RH_1_2_1(i,:) SWin_1_1_1(i) ];
Square brackets are a concatenation operator. What do you expect to happen when you concatenate a character vector with some numeric data? Remember square brackets are not a list operator, they concaenate data into an array of one homogenous type. What should that homogenous type be: numeric, or character? It can't be both.
The solution is very simple: do not concatenate character data with numeric data. You can FPRINTF the character vector first, e.g.:
fprintf(meteo_file, '%s', DateTime(i,:));
meteo = [DOY(i,:),Ta_1_1_1(i,:)-273.15,TA_1_2_1(i,:)-273.15,Ts_1_1_1(i,:)-273.15,Ts_2_1_1(i,:)-273.15,Ts_3_1_1(i,:)-273.15,RH_1_1_1(i,:),RH_1_2_1(i,:),SWin_1_1_1(i)];
fprintf(meteo_file, [repmat('%6.0f',1,9),'\r'], meteo);
Note that you should be using DATETIME/DURATION objects, rather than using deprecated DATESTR/DATENUM/DATEVEC.
1 commentaire
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!