MATLAB Answers

0

Index exceeds matrix dimensions

Asked by Killian Onwudiwe on 7 Aug 2019
Latest activity Commented on by 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

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.

1 Answer

Answer by 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

Show 1 older comment
David K.
on 8 Aug 2019
The problem is that the string
'*.dat'
literally means to search for all files that are of the type .dat so if the files you want are not .dat files then it will not work. Since you say the files you need are .xlsx, you want your command to be
fileNameArray = dir('*.xlsx');
I would suggest typing that into the command line first so you can be sure all the files are being read in as expected before running it in the script.
thank you this worked fine for me but gives me another error
Attempt to reference field of non-structure array.
Error in normalcell (line 11)
a1 (:,i)= datStruct.data(1173,:);
P.S. I am a newbie in programming and really dont have deep understanding.
Thank you for helping
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.