一个给电表用的小 MATLAB 报表,每天一个 CSV,列是电表,行是 30 分钟的间隔。在 Neovim 终端里用 matlab -batch run_report 运行。财务今天就要,所以我心情不太好。
2026-10-30 有 3 个间隔,数字是对的。2026-10-31 只有一个间隔,网关大半天都挂了,内容如下,
M101,M102,M103,M104
0.44,0.30,1.05,0.05
这一天的总 kWh 打印出来是 [0.44 0.74 1.79 1.84]。费用也是同样的错法,从左到右越加越多。总量应该就是这一行里的四个数。peak_kwh 和 n_intervals 都是对的,所以只有总量这里不对劲。
function report = meter_report(deltas, cfg)
running = cumsum(deltas);
report.total_kwh = running(end, :);
report.cost = report.total_kwh * cfg.tariff;
report.peak_kwh = max(deltas, [], 1);
report.n_intervals = size(deltas, 1);
end
batches{k} = readmatrix(fullfile(dataDir, names{k}));
size(batches{k}) 是 1x4,所以文件读取没问题。我怀疑 readmatrix 遇到表头加一行数据时有什么奇怪的行为(docs),或者是这个原生 Apple silicon 版本表现不一样。还没有人在别的机器上跑过。我试过 format long、重启 MATLAB、删掉表头,都没变化。
文件排序用的是标准库里的 natsortfiles,所以不是它的问题。
为什么只有这一天?代码一样,格式也一样。有人遇到过吗?
从英语翻译·查看原文Small MATLAB report for electricity meters, one CSV per day, columns = meters, rows = 30 min intervals. Run it with matlab -batch run_report from Neovim terminal. Finance wants it today, so I am not in good mood.
2026-10-30 has 3 intervals and numbers are right. 2026-10-31 has only one interval, gateway was down most of the day:
M101,M102,M103,M104
0.44,0.30,1.05,0.05
Total kWh for this day prints [0.44 0.74 1.79 1.84]. Cost is wrong the same way, growing from left to right. Total should be just the four numbers from the row. peak_kwh and n_intervals are correct, so something is off only in totals.
function report = meter_report(deltas, cfg)
running = cumsum(deltas);
report.total_kwh = running(end, :);
report.cost = report.total_kwh * cfg.tariff;
report.peak_kwh = max(deltas, [], 1);
report.n_intervals = size(deltas, 1);
end
batches{k} = readmatrix(fullfile(dataDir, names{k}));
size(batches{k}) gives 1x4, so file is read fine. I suspect readmatrix does something strange with header plus one data line (docs), or this native Apple silicon build behaves differently. Nobody ran this on another machine yet. I tried format long, restarting MATLAB, deleting the header. Nothing changed.
Sorting of files is by natsortfiles from standard library, so that is not the problem.
Why only this day? Same code, same format. Anyone seen this?