Plot did not work

M 204 20795 20795 3000 0 16067 16066 3000 0 0 0 6000 0
M 524 20795 20794 3000 0 16067 16066 3000 0 0 0 6000 0
M 735 20795 20795 3000 0 16067 16066 3000 0 0 0 6000 0
M 995 20795 20795 3000 0 16067 16067 3000 0 0 0 6000 0
M 995 20795 20795 3000 0 16067 16067 3000 0 0 0 6000 0
M 1233 20795 20795 3000 0 16067 16067 3000 0 0 0 6000 0
M 1499 20795 20795 3000 0 16067 16067 3000 0 0 0 6000 0
M 1763 20795 20795 3000 0 16067 16067 3000 0 0 0 6000 0
This is the data file. I want to take out 3rd column and 7th column from the file and plot them as well. I write down the code in matlab.
f = fopen('robot4_motors.txt');
plot(f(:, 3), f(:, 7))
But the code did not work .
Throws error Index in position 2 exceeds array bounds (must not exceed 1).

댓글 수: 1

madhan ravi
madhan ravi 2018년 10월 5일
편집: madhan ravi 2018년 10월 5일
Can you upload your text file(robot4_motors.txt)?

답변 (2개)

Steven Lord
Steven Lord 2018년 10월 5일

1 개 추천

The output from fopen is not the data stored in the file. It is a file identifier that you can pass into other functions to allow them to access the data stored in the file.
I'd probably use the readtable or textscan functions to read the data from the file into memory. See the documentation, doc readtable or doc textscan, for information on how to call them and examples you copy into your code and modify to adapt to your data file. Then you can plot the data you read in.
Star Strider
Star Strider 2018년 10월 5일

0 개 추천

Your ‘f’ matrix appears to be a cell array.
If so, and if I am correct in characterising it here, this will run without errors, although (with the data you posted) will not provide an interesting plot:
f = {{'M' 204 20795 20795 3000 0 16067 16066 3000 0 0 0 6000 0
'M' 524 20795 20794 3000 0 16067 16066 3000 0 0 0 6000 0
'M' 735 20795 20795 3000 0 16067 16066 3000 0 0 0 6000 0
'M' 995 20795 20795 3000 0 16067 16067 3000 0 0 0 6000 0
'M' 995 20795 20795 3000 0 16067 16067 3000 0 0 0 6000 0
'M' 1233 20795 20795 3000 0 16067 16067 3000 0 0 0 6000 0
'M' 1499 20795 20795 3000 0 16067 16067 3000 0 0 0 6000 0
'M' 1763 20795 20795 3000 0 16067 16067 3000 0 0 0 6000 0}};
x = [f{:}{:,3}]
y = [f{:}{:,7}]
plot(x, y, '-p')
Experiment to get the result you want.

이 질문은 마감되었습니다.

제품

릴리스

R2017a

태그

질문:

2018년 10월 5일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by