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.
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

标签

关闭:

2021-8-20

Community Treasure Hunt

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

Start Hunting!

Translated by