You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Octopus Agile API返回的结构体中提取完整的value_inc_vat列?

解决MATLAB提取结构体数组字段完整列的问题

在MATLAB中,api_call.results是结构体数组,直接使用.value_inc_vat只会返回数组第一个元素的对应字段值。要提取所有元素的value_inc_vat字段,你可以用以下两种方法:

方法1:结构体数组索引拼接

这是最直接高效的方式,通过冒号索引遍历整个结构体数组,再拼接所有字段值:

clc
current_date = string(datetime('today','Format','uuuu-MM-dd'))
yesterdays_date = string(datetime('yesterday','Format','uuuu-MM-dd'))

api_url = append('https://api.octopus.energy/v1/products/AGILE-24-04-03/electricity-tariffs/E-1R-AGILE-24-04-03-C/standard-unit-rates/?period_from=', yesterdays_date ,'T22:00Z&period_to=' , current_date ,'T23:59Z');

api_call = webread(api_url)

costs = api_call.results
% 修改这行代码提取完整字段列
value_inc_vat = [api_call.results(:).value_inc_vat];
  • 解释:results(:)将结构体数组转换为列向量,确保遍历所有元素;方括号[]会把每个元素的value_inc_vat值拼接成一个一维数组,就是你需要的完整列数据。

方法2:使用arrayfun函数

如果需要处理更复杂的字段提取逻辑,arrayfun是通用方案:

% 替换提取字段的代码为:
value_inc_vat = arrayfun(@(item) item.value_inc_vat, api_call.results);
  • 解释:匿名函数@(item) item.value_inc_vat会对api_call.results中的每个结构体元素执行字段提取,arrayfun自动收集所有结果为数组。

完成修改后,你可以用disp(value_inc_vat)查看提取到的所有含税电价数据,确认是否覆盖了API返回的全部时段。

内容的提问来源于stack exchange,提问作者Alex Band

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 09:09:52