I'm trying to create a bar chart for a matrix, its a 1x30 but I only want the 0, 10th, 20th and 30th values.
I've tried many formats such as in the image but I cannot manage to get it working.
Any suggestions out there?

 채택된 답변

Ameer Hamza
Ameer Hamza 2018년 5월 4일
편집: Ameer Hamza 2018년 5월 4일

1 개 추천

I MATLAB, use the following syntax to index into an array.
bar([1 10 20 30], mean_1_1_walk([1 10 20 30]))
Also in MATLAB indexing start from 1, unlike some other programming languages (such as c/c++, java, python) where indexing starts from 0.

댓글 수: 8

dpb
dpb 2018년 5월 4일
"... unlike most other programming languages."
"... unlike one other particular programming language."
Jordanf1519
Jordanf1519 2018년 5월 4일
That sorted it, thank you.
dpb
dpb 2018년 5월 4일
Also note you can write expressions like
mean_1_1_walk([1:10:end])
besides a list of specific indices.
Jordanf1519
Jordanf1519 2018년 5월 4일
Would you also happen to know how to have the bars side by side instead of stacked when plotting more than one matrix?
@dpb OP is indexing them like [1 10 20 30] for some reason. Therefore I wrote it like that.
@Jordanf1519 refer to this example. For your case the command becomes
bar([1 10 20 30], [mean_1_1_walk([1 10 20 30])' mean_1_2_walk([1 10 20 30])']);
dpb
dpb 2018년 5월 4일
Indeed he did, but he also started counting from 0 instead of 1. It's quite possible he didn't know could write an expression; was simply giving alternate syntax as tutorial additional information.
Jordanf1519
Jordanf1519 2018년 5월 5일
Would anyone know how to change the colours of these bars too?
bar([1 10 20 30], [mean_1_1_walk([1 10 20 30])' mean_1_2_walk([1 10 20 30])']);
Ameer Hamza
Ameer Hamza 2018년 5월 5일
You can find several examples on documentation page which will help you to do this: https://www.mathworks.com/help/matlab/ref/bar.html#d119e66370.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

질문:

2018년 5월 4일

댓글:

2018년 5월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by