Index exceeds matrix dimensions
古いコメントを表示
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
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.
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
2019 年 8 月 7 日
David K.
2019 年 8 月 8 日
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
2019 年 8 月 8 日
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 についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!