I was trying to graph this text file, it keeps on giving me index must not exceed array bounds error, please help thank you.
clc
clear all
A = fopen('c1s1.txt','rt');
time = A(:,2);
encode1 = A(:,3);
plot(time,encode1)
title('Time vs Encoder 1 PoS')

 Akzeptierte Antwort

Star Strider
Star Strider am 29 Apr. 2022

0 Stimmen

The ‘A’ variable is actually the assigned file identification number, always a scalar. It us used to refer to the file using fscanf, textscan, or certain other file import functions.
Your code needs to read the file. Here is one way to do it, there are several others —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/982340/c1s1.txt', 'VariableNamingRule','preserve')
T1 = 1807×5 table
Var1 Var2 Var3 Var4 Var5 __________ ____ _____ ______ __________ {'[' } 0 0 927.47 {'0.000;'} {0×0 char} 1 0.004 927.47 {'0.000;'} {0×0 char} 2 0.009 927.47 {'0.000;'} {0×0 char} 3 0.013 927.47 {'0.000;'} {0×0 char} 4 0.018 927.47 {'0.000;'} {0×0 char} 5 0.022 927.47 {'0.000;'} {0×0 char} 6 0.027 927.47 {'0.000;'} {0×0 char} 7 0.031 926 {'0.000;'} {0×0 char} 8 0.035 926 {'0.000;'} {0×0 char} 9 0.04 926 {'0.000;'} {0×0 char} 10 0.044 926 {'0.000;'} {0×0 char} 11 0.049 926 {'0.000;'} {0×0 char} 12 0.053 926 {'0.000;'} {0×0 char} 13 0.058 925.44 {'0.000;'} {0×0 char} 14 0.062 925.28 {'0.000;'} {0×0 char} 15 0.066 925.12 {'0.000;'}
% Q1 = T1(end-10:end,:)
time = T1{:,2};
encode1 = T1{:,3};
figure
plot(time,encode1)
xlabel('Time')
ylabel('Encoder 1 PoS')
title('Time vs Encoder 1 PoS')
Var4 = T1{:,4};
figure
plot(time, Var4)
xlabel('Time')
ylabel('Var_4')
Plotting ‘Var4’ (that I did just to see what it was) is a bit more interesting.
.

5 Kommentare

Kevin Antony
Kevin Antony am 29 Apr. 2022
thank you so much, appreciate it a lot ,so the variable A was the component causing the issue?
Star Strider
Star Strider am 29 Apr. 2022
As always, my pleasure!
Variable ‘A’ was exactly what it was supposed to be, that being the file identification number. It just needed to be used to read the file, and that is easier (especially with this file) with readtable (although readmatrix would have worked as well, I generally use readtable because it displays the header information as variable names, helping me understand what’s in the file).
Kevin Antony
Kevin Antony am 29 Apr. 2022
very sorry to distrub again, but as i am trying to graph this MATLAB says it cannot find the file, check path or permissions, i regrouped everything into MATLAB drive but still cannot get it to read, thank you
Kevin Antony
Kevin Antony am 29 Apr. 2022
nevermind i figured it out, thank you so much for all the help
No worries!
That depends on where you‘re trying to access it.
Accessing it here is straightforward:
M1 = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/982340/c1s1.txt')
M1 = 1807×5
NaN 0 0 927.4690 NaN NaN 1.0000 0.0040 927.4690 NaN NaN 2.0000 0.0090 927.4690 NaN NaN 3.0000 0.0130 927.4690 NaN NaN 4.0000 0.0180 927.4690 NaN NaN 5.0000 0.0220 927.4690 NaN NaN 6.0000 0.0270 927.4690 NaN NaN 7.0000 0.0310 926.0000 NaN NaN 8.0000 0.0350 926.0000 NaN NaN 9.0000 0.0400 926.0000 NaN
That should also work from your computer, so long as you’re connected to the Internet.
The NaN values (that do not appear as such in the table) are due to their not being numeric.
If you want to read it on your computer, MATLAB has to know where it is. If it’s on your MATLAB path, the full path can be obtained by:
fulpath = which('c1s1.txt')
Otherwise, you need to put it on your MATLAB path (see What Is the MATLAB Search Path?) or use the fullfile function to create a path to it.
Also, I believe the correct plot should be:
figure
plot(M1(:,3), M1(:,4))
grid
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by