Code that is working in console, not working in the script?

4 views (last 30 days)
Hello, I have a code that draws a bar chart for each month.
clf;
hold on;
len=length(in_mon); %in_mon is a vector of months i.e [2 4 1]
%there is data for 2012 and 2013, which is why there's two sets of datas
[pAvg1, pAvg2]=cal_month(len,date_1, date_2,wcf_1,wcf_2, in_mon); %returns the average for each month that was requested in in_mon with 2 vectors of len length
axis([0 len min(min(pAvg1), min(pAvg2)) max(max(pAvg1), max(pAvg2))]);
y=zeros(len,2);
for n=1:len %for loop is for orienting data properly for drawing bar
y(n,1)=pAvg1(n);
y(n,2)=pAvg2(n);
end
bar(in_mon',y);
xlabel('Month');
ylabel('WCF');
title('Average WCF');
legend('2012','2013');
^ This is the result of running the script, as you can see, no graph
^ This is the result of writing some of the code in console (using values that have already been defined with the previous script run). As you can see, that is a proper result.
I have no idea what is causing the script to fail, when the console seems to work. Any fixes?

Accepted Answer

DGM
DGM on 4 Jan 2022
Edited: DGM on 4 Jan 2022
The values passed to axis() are setting the x-limits such that the bars are no longer in-frame (and such that the bottoms of the bars would be cut off). This doesn't even appear to be necessary anyway, but I don't have your data, so maybe this simplification suffices.
in_mon = [9 10 11 12];
pAvg1 = rand(1,4);
pAvg2 = rand(1,4);
y = [pAvg1.' pAvg2.']; % no loop
bar(in_mon',y);
xlabel('Month');
ylabel('WCF');
title('Average WCF');
legend('2012','2013');

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!