"Variable index exceeds table dimensions" Error

63 次查看(过去 30 天)
Hello,
I have a MATLAB code that is ued for converting data from a machine to numerical values (I have not developed the code, provided by company). When I'm trying to run the code, it keeps repeating the error message of 'Variable index exceeds table dimensions" even after changing the matrix dimensions in the code. I am attaching the code as well as the machine generated .csv file which is to be converted. Can someone please help remove this error?
Regards,
Ankit
% Controller captures the data for 6 seconds with 500Hz frequency [3000 data points]
clear all; close all; clc;
filename=uigetfile('.csv');
data=readtable(filename);
data(1:2,:)=[]; % get rid of first two rows which contain strings
data_test=data(1:30000,4); % getting rid of testing info data at the end of table
data_test=strrep(table2array(data_test),',','.'); % convert to cell containing strings and replace comma with dot
data_test=str2double(data_test); % conversion to double
% we have 10 variables ... each variable is saved at 3000 points
data_real=reshape(data_test,[3000,10]); % reshape into block
%%
%{
1. energy
2. IstStrom_VM [actual power of prestroke motor]
3. IstDrehmoment_RM [actual torque friction motor]
4. IstStrom_RM [actual power friction motor]
5. s_abs_ist
6. s_rel_ist
7. Axialkraft_ist [axial force measure from bit]
8. IstGeschwindigkeit_RM [speed friction motor]
9. Stufennummer [step number]
10. Zeit [time]
%}
%% Sort data
% [1.Time 2.StepNumber 3.Energy 4.AxialForce 5.SpeedFrictionMotor 6.TorqueFrictionMotor
% 7.PowerFrictionMotor 8.PowerPreStrokeMotor 9.AbsDisp 10.RelDisp]
data_sorted=[data_real(:,10) data_real(:,9) data_real(:,1) data_real(:,7) data_real(:,8) data_real(:,3)...
data_real(:,4) data_real(:,2) data_real(:,5) data_real(:,6)];
%% save processed file into excel sheet
data_table = array2table(data_sorted,...
'VariableNames',{'Time_sec','StepNumber','Energy_J','Axial_Force_N','Speed_FM_RPM',...
'Torque_FM_Nm','Power_FM_', 'Power_PM_','Abs_Disp_mm','Rel_Disp_mm'});
filename=erase(filename,'.csv');
% writetable(data_table,sprintf('Proc_%s.xlsx',filename));
%% Plotting
idx=max(find(data_sorted(:,2)==4)); % draw till step 4
%figure('units','normalized','outerposition',[0 0 1 1])
figure
subplot(2,1,1)
plot(data_sorted(1:idx,1),data_sorted(1:idx,6),'LineWidth',1.25)
xlim([data_sorted(1,1) , data_sorted(idx,1)])
xlabel('Time'); ylabel('Torque (Nm)');
%grid on
hold on
plot(data_sorted(1:idx,1),data_sorted(1:idx,2),'r','LineWidth',1.25)
% Draw a line right at the end of Step 3
%line([data_sorted(max(find(data_sorted(:,2)==3)),1),data_sorted(max(find(data_sorted(:,2)==3)),1)],[-5 14],...
% 'LineWidth',1.25,'Color','Black')
% legend('Torque','Step Number')
set(gca,'FontSize',15)
subplot(2,1,2)
plot(data_sorted(1:idx,1),data_sorted(1:idx,5),'LineWidth',1.25)
xlim([data_sorted(1,1) , data_sorted(idx,1)])
xlabel('Time'); ylabel('Speed Friction Motor (RPM)'); ylim([0 9000])
%grid on
set(gca,'FontSize',15)
figure
plot(data_sorted(1:idx,1),data_sorted(1:idx,6),'LineWidth',1.25)
xlim([data_sorted(1,1) , data_sorted(idx,1)])
xlabel('Time'); ylabel('Torque (Nm)'); ylim([-15,15])
%grid on
hold on
plot(data_sorted(1:idx,1),data_sorted(1:idx,2),'r','LineWidth',1.25)
% Draw a line right at the end of Step 3
%line([data_sorted(max(find(data_sorted(:,2)==3)),1),data_sorted(max(find(data_sorted(:,2)==3)),1)],[-5 14],...
% 'LineWidth',1.25,'Color','Black')
% legend('Torque','Step Number')
set(gca,'FontSize',15)
figure
plot(data_sorted(1:idx,1),data_sorted(1:idx,5),'LineWidth',1.25)
xlim([data_sorted(1,1) , data_sorted(idx,1)])
xlabel('Time'); ylabel('Speed Friction Motor (RPM)'); ylim([0 9000])
%grid on
set(gca,'FontSize',15)
time_Step2=data_sorted(max(find(data_sorted(:,2)==2)),1)-data_sorted(min(find(data_sorted(:,2)==2)),1);
time_Step3=data_sorted(max(find(data_sorted(:,2)==3)),1)-data_sorted(min(find(data_sorted(:,2)==3)),1);
fprintf('Time Duration of Step 2 is %f\n',time_Step2)
fprintf('Time Duration of Step 3 is %f\n',time_Step3)
  5 个评论
Jan
Jan 2018-11-27
Which line is "line 9"? Prefer to post the complete error message.
ankit varma
ankit varma 2018-11-27
@ All- The problem is not with the code but with the version of MATLAB I have been using. The code is running fine when R2017a version is used. Appreciate your help!

请先登录,再进行评论。

采纳的回答

Jan
Jan 2018-11-27
If the failing line is:
data_test=data(1:30000,4);
use the debugger to find the cause. Type this in the command window:
dbstop if error
Now start the code again. When Matlab stops at the error, check the size of the used variable:
size(data)
What do you observe? I guess, that data does not have 4 columns.

更多回答(4 个)

quaim abbas
quaim abbas 2021-6-2
I am performing code on thingspeak to compare two body vitals in 1 chart .but when i write the code it give me error saying that variable index exceeds table dimensions giving error in this line"sensorData(sensorData{:,3}>100,:)=[];"
startTime = datetime(2021,4,16,04,06,57);
endTime= startTime+ days(2);
sensorData = thingSpeakRead(1357008,'Location',1,'dateRange',[startTime endTime],...
'location',1,'outputformat','timetable');
sensorData(sensorData{:,3}>100,:)=[];
plot (sensorData.Timestamps,sensorData.TemperatureF)
ylabel('^0F');
hold;
yyaxis right
plot(sensorData.Timestamps,sensorData.SoilMoisture);
ylabel('Soil Conductivity');
ylim([700 900]);
hold off;
  2 个评论
Walter Roberson
Walter Roberson 2021-6-2
There are no samples for that time range on that channel, but your code is not testing isempty(sensorData)

请先登录,再进行评论。


Roya Kakar
Roya Kakar 2022-1-19
By running the folowing line of code I get the following error message
X=X(:, I);
Error message: Variable index exceeds table dimensions.
  1 个评论
Walter Roberson
Walter Roberson 2022-1-19
X=X(:, I);
Suppose X is a table, and suppose you are looping over I values.
The first iteration, when I is 1, that would be
X=X(:,1)
which would replace the table X with a table that had only a single variable -- the first variable in the original X table.
The second iteration, when I is 2, that would be
X=X(:,2)
but remember that you replaced the original table X with a new table that only had one variable, so trying to index variable number 2 from it is not going to work as you discarded all variables after the first variable in the first iteration.

请先登录,再进行评论。


Roya Kakar
Roya Kakar 2022-1-19
This is my actual code
X = readtable('lingspam_features.csv');
sm =(table2array(X));
[B, I] = sort(sm, 'desc');
X=X(:, I);
X=X(:, 1:500);
Error message: and this line ( X=X(:, I);) shows this error message: Variable index exceeds table dimensions.
  3 个评论
Walter Roberson
Walter Roberson 2022-1-19
X = array2table(randi(9,5,7))
X = 5×7 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 ____ ____ ____ ____ ____ ____ ____ 1 6 4 3 6 5 3 8 7 3 7 5 3 6 7 5 9 6 2 8 7 8 7 2 1 4 5 4 9 7 4 4 5 4 7
sm = table2array(X)
sm = 5×7
1 6 4 3 6 5 3 8 7 3 7 5 3 6 7 5 9 6 2 8 7 8 7 2 1 4 5 4 9 7 4 4 5 4 7
[B, I] = sort(sm, 'desc')
B = 5×7
9 7 9 7 6 8 7 8 7 4 6 5 5 7 8 7 4 4 5 5 6 7 6 3 3 4 4 4 1 5 2 1 2 3 3
I = 5×7
5 2 3 2 1 3 3 2 4 1 3 2 1 5 4 5 5 5 5 4 2 3 1 2 1 4 5 4 1 3 4 4 3 2 1
X(:,I)
ans = 5×35 table
Var5 Var2 Var4 Var3 Var1 Var2_1 Var4_1 Var5_1 Var1_1 Var3_1 Var3_2 Var1_2 Var5_2 Var2_2 Var4_2 Var2_3 Var3_3 Var5_3 Var1_3 Var4_3 Var1_4 Var2_4 Var5_4 Var4_4 Var3_4 Var3_5 Var1_5 Var4_5 Var5_5 Var2_5 Var3_6 Var5_6 Var2_6 Var4_6 Var1_6 ____ ____ ____ ____ ____ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ 6 6 3 4 1 6 3 6 1 4 4 1 6 6 3 6 4 6 1 3 1 6 6 3 4 4 1 3 6 6 4 6 6 3 1 5 7 7 3 8 7 7 5 8 3 3 8 5 7 7 7 3 5 8 7 8 7 5 7 3 3 8 7 5 7 3 5 7 7 8 2 5 6 9 7 5 6 2 7 9 9 7 2 5 6 5 9 2 7 6 7 5 2 6 9 9 7 6 2 5 9 2 5 6 7 4 7 1 2 8 7 1 4 8 2 2 8 4 7 1 7 2 4 8 1 8 7 4 1 2 2 8 1 4 7 2 4 7 1 8 5 7 4 4 9 7 4 5 9 4 4 9 5 7 4 7 4 5 9 4 9 7 5 4 4 4 9 4 5 7 4 5 7 4 9
This shows that when the number of rows is less than the number of columns that the indices happen to fall within range of the number of columns, so the indexing succeeds. But are you sure that you want that output, with columns repeated as many times as there are rows ??
I would suggest to you that you do not want to sort() the entire array sm and instead want to sort one single row out of it.
Roya Kakar
Roya Kakar 2022-1-19
Thank you so much for your detailed answer. I will take your comments into consideration

请先登录,再进行评论。


Eirik
Eirik 2023-2-9
I = readtable('LindesnesSolenergi.csv','Delimiter',';', 'PreserveVariableNames',1); % Importere alle kolonner fra CSV-fil
I = table2array(I(1:8760,2))./1000;
Error using ()
Variable index exceeds table dimensions.
Error in Lindesnes (line 2)
I = table2array(I(1:8760,2))./1000;
  1 个评论
Walter Roberson
Walter Roberson 2023-2-9
Your file LindesnesSolenergi.csv does not have at least 8760 rows after readtable() removed what it thought were headers.
We do not have that file to test with so we do not know the actual size of the file.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by