Problem with writing datestr converted date time to a text file alongside other numeric variables
조회 수: 6 (최근 30일)
이전 댓글 표시
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
Adam Danz
2023년 2월 22일
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.
채택된 답변
Stephen23
2023년 2월 22일
편집: Stephen23
2023년 2월 22일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!