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

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?

 採用された回答

DGM
DGM 2022 年 1 月 4 日
編集済み: DGM 2022 年 1 月 4 日

1 投票

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');

1 件のコメント

Chinguun Erdenebadrakh
Chinguun Erdenebadrakh 2022 年 1 月 4 日
Thanks for the help! I have long ways to go

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

製品

リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by