MATLAB Answers

Index Exceeds Matrix Dimensions

1 view (last 30 days)
James on 24 Dec 2019
Commented: James on 26 Dec 2019
Apologies in advance for posting a common error/problem but I couldn't find relevant solutions in other simliar threads.
So I'm trying out this code which I found online and it gives the above mentioned error at:
[data, txt] = xlsread('sample.xlsx');
[m,n] = size(data) = data(:,1);
srdata.Longitude = data(:,2);
srdata.Lattitude = data(:,3);
srdata.altitude = data(:,4);
for i=1:m
srdata.Description{i,1} = txt{i,5};
error line points at "srdata.Description{i,1} = txt{i,5};" and says "Index Exceeds Matrix Dimensions".
I wanted to know why am I getting this and how to fix this, if possible.
P.S: sample.xlsx is something like this.
Thanks in advance!


Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 24 Dec 2019
T = readtable('sample.xlsx','ReadVariableNames',0);
T.Properties.VariableNames = {'data','Longitude','Lattitude','altitude','Description'};
srdata = table2struct(T);


James on 24 Dec 2019
Thank you very much!
I was wondering whether there is another way to alter the old code without replacing it to get over the error?
Andrei Bobrov
Andrei Bobrov on 24 Dec 2019
[data, txt] = xlsread('sample.xlsx');
m = size(data,1);
for i = 1:m
srdata(i).data = data(i,1);
srdata(i).Longitude = data(i,2);
srdata(i).Lattitude = data(i,3);
srdata(i).altitude = data(i,4);
srdata(i).Description = txt{i};
James on 26 Dec 2019
Thank you so much!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!