how to code irregular time periods??

9 visualizzazioni (ultimi 30 giorni)
Boss Man
Boss Man il 6 Feb 2020
Risposto: Darshak il 24 Giu 2025
i have the following data:
a = xlsread('outdoorall.xlsx','G2:H52364');
b=xlsread('indoorall.xlsx','G2:H52364'); %2397
time=xlsread('outdoorall.xlsx','G2:G52364');
for i=1:2364
dt=(b(i+1)-b(i)); % change in indoor temperature divided by time
c=dt/a(i)-b(i);
plot(c)
end
the problem is my time periods are irregular so im struggling to code DT above as idk how to set time??? i dont want to use xlsread due to this
i was told to find a function or code it by myself but idk understand matlab very well
also is my for loop correct??

Risposte (1)

Darshak
Darshak il 24 Giu 2025
This kind of issue is common when working with time-based data from Excel, especially when the time intervals are irregular or stored in non-standard formats. A lot of times, Excel time columns like "00.05.00" come in as strings, and functions like “xlsread” do not help much — switching to “readtable” makes it easier to handle the data properly.
In your case, where the goal is to compute the rate of change of indoor temperature and relate it to the outdoor temperature, this can be done reliably by following a few clear steps.
Here is how the workflow can be structured:
  • Step 1: Import the data using “readtable” with a specified range. This avoids reading unnecessary rows and keeps things clean.
  • Step 2: Convert the time column (which appears in HH.MM.SS format in the spreadsheet) to numeric using “str2double”, assuming it represents decimal hours. This conversion is necessary for arithmetic and interpolation.
  • Step 3: Clean the data by removing any rows where time or temperature values could not be parsed and resulted in “NaN”.
  • Step 4: Remove duplicate timestamps from the outdoor data. “interp1” requires strictly increasing time values, so repeated timestamps will cause an error otherwise.
  • Step 5: Interpolate the outdoor temperature values to the indoor time points using “interp1”. This aligns the two datasets in time even if their sampling rates or times do not match.
  • Step 6: Compute the rate of change of indoor temperature using diff and divide it by the difference in time between samples.
  • Step 7: Apply the transformation you described, which combines rate of change, outdoor temperature, and indoor temperature.
The key parts of this implementation look like:
indoorT = readtable('indoorapr.xlsx', 'Range', 'B2:D2398', 'ReadVariableNames', false);
outdoorT = readtable('outdoorapr.xlsx', 'Range', 'B2:D52365', 'ReadVariableNames', false);
indoor_time = str2double(indoorT{:,2});
indoor_temp = indoorT{:,3};
outdoor_time = str2double(outdoorT{:,2});
outdoor_temp = outdoorT{:,3};
valid_indoor = ~isnan(indoor_time) & ~isnan(indoor_temp);
indoor_time = indoor_time(valid_indoor);
indoor_temp = indoor_temp(valid_indoor);
valid_outdoor = ~isnan(outdoor_time) & ~isnan(outdoor_temp);
outdoor_time = outdoor_time(valid_outdoor);
outdoor_temp = outdoor_temp(valid_outdoor);
[outdoor_time_unique, idx] = unique(outdoor_time);
outdoor_temp_unique = outdoor_temp(idx);
outdoor_interp = interp1(outdoor_time_unique, outdoor_temp_unique, indoor_time, 'linear', 'extrap');
delta_temp = diff(indoor_temp);
delta_time = diff(indoor_time);
rate_change = delta_temp ./ delta_time;
indoor_trimmed = indoor_temp(1:end-1);
outdoor_interp_trimmed = outdoor_interp(1:end-1);
c = (rate_change ./ outdoor_interp_trimmed) - indoor_trimmed;
We can plot the result with a simple plot(c) or export it as needed. The full pipeline should be robust for irregular time sampling if the decimal hour values are formatted consistently in the spreadsheet.
The following documentation links can be referred to for a better understanding:

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by