MATLAB: “Variable index exceeds table dimensions” Error

errorMATLABmatrix dimensions

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)

Best Answer

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