Index exceeds matrix dimensions

1 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2019-8-7
Numbering variables like that is a sign that you are doing something wrong.
Using indexing makes code simpler and more reliable.

请先登录,再进行评论。

回答(1 个)

David K.
David K. 2019-8-7
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 个评论
Killian Onwudiwe
Killian Onwudiwe 2019-8-8
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
Walter Roberson
Walter Roberson 2019-8-8
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()

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by