How can you plot the output of a for loop?

2 visualizzazioni (ultimi 30 giorni)
I know I should make an array of all the output and then plot it, but when i make an array for 2010:2019, I also get the output for everyting lower than 2010. (TG is a 3635x1 dataset and the function works 100%
My function:
function Tgem = JaarGemiddelde(jaartal)
load data_weer.mat
Tgem=mean(TG(jaar==jaartal)./10);
A different script:
for b=2010:2019;
a(b)=JaarGemiddelde(b)
end

Risposta accettata

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 14 Gen 2020
Hi,
if I have understood your exercise correctly, you'd like to plot the year vs. average values of the variable TG. If so here is the complete script that you can improve a bit more w.r.t your problem statements:
b=2010:2019;
for ii=1:numel(b)
A(ii)=JaarGemiddelde(b(ii));
end
plot(b, A), xlabel('\it year'), ylabel('\it Average of data')
function Tgem = JaarGemiddelde(jaartal)
load data_weer.mat
Tgem=mean(TG(jaar==jaartal)./10);
end
Are you using later versions of MATLAB (2018b or later ver)? If so, the above code works very well. Otherwise, you'd need to create your fcn file in separate - JaarGemiddelde.m that you'd need to call it from another m-file or command window with:
b=2010:2019;
for ii=1:numel(b)
A(ii)=JaarGemiddelde(b(ii));
end
plot(b, A), xlabel('\it year'), ylabel('\it Average of data')
Good luck.
  2 Commenti
Bram van der Horst
Bram van der Horst il 14 Gen 2020
The script works perfectly.
I also see where I went wrong.
Thank you for your time and effort!!
Sulaymon Eshkabilov
Sulaymon Eshkabilov il 14 Gen 2020
It is just a pleasure to be of some help. Good luck.

Accedi per commentare.

Più risposte (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 14 Gen 2020
Can you share your data file in order to be able to address your question correctly?
  1 Commento
Bram van der Horst
Bram van der Horst il 14 Gen 2020
Here's the .mat file: https://drive.google.com/file/d/1teUj7SL6H8Ic08bTYEjW96ITLAfTujIj/view?usp=sharing

Accedi per commentare.


Sulaymon Eshkabilov
Sulaymon Eshkabilov il 14 Gen 2020
I see your mat contains bunch of data. All variables are of the same size 3635-by-1. So you wish to plot what variable vs. what variable from your mat file? Are there any pre-conditions that must be respected while plotting the data?
I believe that you know how to plot any of them vs. any other, then it is straightforward. plot(dag, TG), plot(RH, TG), ...
  1 Commento
Bram van der Horst
Bram van der Horst il 14 Gen 2020
I have to plot the year in which the data was aquired (so 2010-2019) vs the average of that year (which I achieve by using the function which takes the average from TG)

Accedi per commentare.

Categorie

Scopri di più su Line Plots 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