MATLAB Answers

Index exceeds matrix dimensions

1 view (last 30 days)
Killian Onwudiwe
Killian Onwudiwe on 7 Aug 2019
Commented: Walter Roberson on 8 Aug 2019
please how do I solve the problem of "Index exceeds matrix dimensions".
it keeps return an error on this line "datStruct = importdata(fileNameArray(i).name);" showing that fileNamearray has a strcuture of 0x1 and the index i has a 1x1 structure
below is my code
% shear strain 10A_4_4_55+2_4 right cell
clc;clear;
fileNameArray = dir('*.dat');
for i = 1:50
datStruct = importdata(fileNameArray(i).name);
a1 (:,i)= datStruct.data(1173,:);
a2 (:,i)= datStruct.data(1175,:);
a3 (:,i)= datStruct.data(1167,:);
a4 (:,i)= datStruct.data(1181,:);
a5 (:,i)= datStruct.data(1197,:);
a6 (:,i)= datStruct.data(1213,:);
a7 (:,i)= datStruct.data(1250,:);
a8 (:,i)= datStruct.data(1309,:);
a9 (:,i)= datStruct.data(1369,:);
a10 (:,i)= datStruct.data(1425,:);
a11 (:,i)= datStruct.data(1448,:);
a12 (:,i)= datStruct.data(1469,:);
a13 (:,i)= datStruct.data(1478,:);
a14 (:,i)= datStruct.data(1476,:);
a15 (:,i)= datStruct.data(1474,:);
a16 (:,i)= datStruct.data(1445,:);
a17 (:,i)= datStruct.data(1410,:);
a18 (:,i)= datStruct.data(1372,:);
a19 (:,i)= datStruct.data(1311,:);
a20 (:,i)= datStruct.data(1233,:);
end;
%{
nucleus
a1 (:,i)= datStruct.data(1275,:);
a2 (:,i)= datStruct.data(1277,:);
a3 (:,i)= datStruct.data(1281,:);
a4 (:,i)= datStruct.data(1285,:);
a5 (:,i)= datStruct.data(1315,:);
a6 (:,i)= datStruct.data(1317,:);
a7 (:,i)= datStruct.data(1321,:);
a8 (:,i)= datStruct.data(1325,:);
a9 (:,i)= datStruct.data(1359,:);
a10 (:,i)= datStruct.data(1363,:);
a11 (:,i)= datStruct.data(1367,:);
a12 (:,i)= datStruct.data(1398,:);
a13 (:,i)= datStruct.data(1402,:);
a14 (:,i)= datStruct.data(1433,:);
a15 (:,i)= datStruct.data(1435,:);
a16 (:,i)= datStruct.data(1439,:);
%}
  1 Comment
Stephen
Stephen on 7 Aug 2019
Numbering variables like that is a sign that you are doing something wrong.
Using indexing makes code simpler and more reliable.

Sign in to comment.

Answers (1)

David K.
David K. on 7 Aug 2019
It appears that for some reason fileNameArray is not being populated with values in the line
fileNameArray = dir('*.dat');
Make sure that you that the .dat files you are looking for are in your working folder. If they are in a folder within the working folder you may need to try
dir('*/*.dat')
or
dir('**/*.dat')
  4 Comments
Walter Roberson
Walter Roberson on 8 Aug 2019
importdata() sometimes returns pure numeric arrays instead of a structure. I avoid using importdata(). You should consider using xlsread() or readtable(); if you have R2019a or later you might be able to use readmatrix()

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by