Plot did not work
Info
This question is closed. Reopen it to edit or answer.
Show older comments
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 Comment
madhan ravi
on 5 Oct 2018
Edited: madhan ravi
on 5 Oct 2018
Can you upload your text file(robot4_motors.txt)?
Answers (2)
Steven Lord
on 5 Oct 2018
1 vote
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
on 5 Oct 2018
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.
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!