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일

0 개 추천

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월 7일
Thank you David K. My working folder has the files I am looking for (though they are in xlsx form) I have also tried to convert it to csv files but yet it gives me the same challenge
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.
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()

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2019년 8월 7일

댓글:

2019년 8월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by