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 Kommentar

madhan ravi
madhan ravi am 5 Okt. 2018
Bearbeitet: madhan ravi am 5 Okt. 2018
Can you upload your text file(robot4_motors.txt)?

Antworten (2)

Steven Lord
Steven Lord am 5 Okt. 2018

1 Stimme

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 am 5 Okt. 2018

0 Stimmen

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.

Diese Frage ist geschlossen.

Produkte

Version

R2017a

Gefragt:

am 5 Okt. 2018

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by