User input bar graph

Hey everyone, this is the last part of my Homework and I'm stuck. We are supposed to ask the user to input number of years, then take budget as an input for each year and plot all of this on a bar graph with years X and budget Y. I can't even get my second input to work. Here is what I have so far -
clear;
clc;
clf;
years=input('How many years? ');
for curyear=1:years;
fprintf('Write budget amount for Year #%g: \n','curyear')
end
I'm open to any help. Thanks!

回答 (2 件)

Image Analyst
Image Analyst 2014 年 12 月 3 日

0 投票

The second call to input() should probably be inside your loop over years, since you need to ask for a different amount for each year.

2 件のコメント

Adam
Adam 2014 年 12 月 3 日
I think I am very close. Here is my new code, do you think this will work as I can not test it at the moment?
clear;
clc;
clf;
years=input('How many years? ');
for curyear=1:years;
fprintf('Write budget amount for Year #%g: ',curyear)
budget=input('');
bar(curyear,budget);
hold on
end
Image Analyst
Image Analyst 2014 年 12 月 3 日
Why did you not put the prompt inside input like you did the first time?
userPrompt = sprintf('Write budget amount for Year #%g: ',curyear)
budget=input(userPrompt);

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

Thorsten
Thorsten 2014 年 12 月 3 日

0 投票

If you store your budget for each year you just need a single call of bar
years = input('How many years? ');
for curyear=1:years;
budget(curyear) = input(sprintf('Write budget amount for Year #%g: ',curyear));
end
bar(1:years,budget);
xlabel('Year')
ylabel('Budget')

カテゴリ

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

タグ

質問済み:

2014 年 12 月 2 日

回答済み:

2014 年 12 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by