"Variable index exceeds table dimensions" Error
68 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Jan
on 27 Nov 2018
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.
More Answers (4)
quaim abbas
on 2 Jun 2021
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 Comments
quaim abbas
on 2 Jun 2021
Edited: Walter Roberson
on 2 Jun 2021
here is link from where i am trying to get help i took the code from here.
Walter Roberson
on 2 Jun 2021
There are no samples for that time range on that channel, but your code is not testing isempty(sensorData)
Roya Kakar
on 19 Jan 2022
By running the folowing line of code I get the following error message
X=X(:, I);
Error message: Variable index exceeds table dimensions.
1 Comment
Walter Roberson
on 19 Jan 2022
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
on 19 Jan 2022
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 Comments
Walter Roberson
on 19 Jan 2022
X = array2table(randi(9,5,7))
sm = table2array(X)
[B, I] = sort(sm, 'desc')
X(:,I)
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
on 19 Jan 2022
Thank you so much for your detailed answer. I will take your comments into consideration
Eirik
on 9 Feb 2023
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 Comment
Walter Roberson
on 9 Feb 2023
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.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!