Error Plotting values from a struct with fields
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
XPX Labs
am 29 Aug. 2020
Beantwortet: Bruno Luong
am 29 Aug. 2020
Greetings,
I have been playing with another Trial of Matlab. I decided to try to make an HTTP request and get the data back and plot it.
Solar API Request
This script will recieve data from the National Renewable Energy Labrabtory.
Application Interface
So here we pass:
API Key
API Endpoint related to solar data
nrel = 'https://developer.nrel.gov/'
uri = 'api/solar/solar_resource/v1.json?'
key = 'api_key=GetYourOwnKey'
lat = '&lat=37'
lon = '&lon=-97'
solarData = webread(strcat(nrel,uri,key,lat,lon));
This works just fine (I think). So what is returned is a 1x1 Struct with nested structs inside of it.

So the data I need is outputs:

And finally one more level down:

So as you can see, the struct here has 12 fields with values. When I try to plot it, I get errors. I need help understanding how to plot the montly names along the x axis and the values along the y axis. Any help?
Cheers
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 29 Aug. 2020
values = structfun(@(x) x, solarData.outputs.avg_dni.monthly);
names = fieldnames(solarData.outputs.avg_dni.monthly);
plot(values);
xticks(1:length(values));
xticklabels(names);
Weitere Antworten (1)
Bruno Luong
am 29 Aug. 2020
plot(struct2array(solarData.outputs.avg_dni.monthly))
You can do the label using Walter's solution
0 Kommentare
Siehe auch
Kategorien
Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!