Index exceeds matrix dimensions using sprintf by calling mat files

Can someone help me with the following error?
Index exceeds matrix dimensions.
Error in Lateral_Position (line 28)
eval(sprintf('x=[TP%d_BaseLine(:,2)]',i));
_________________________________________________________________
%%Load Data
filenames = {'TP2_BL.mat','TP3_BL.mat','TP4_BL.mat','TP5_BL.mat','TP6_BL.mat','TP7_BL.mat','TP8_BL.mat','TP9_BL.mat','TP10_BL.mat','TP11_BL.mat','TP12_BL.mat','TP13_BL.mat','TP14_BL.mat','TP15_BL.mat','TP16_BL.mat','TP17_BL.mat','TP18_BL.mat','TP19_BL.mat'};
for kk = 1:numel(filenames)
load(filenames{kk})
end
disp('Cell data BL loaded')
disp('-------------------------------------------------------------------')
Ldist=[];
for i=2:19
eval(sprintf('x=[TP%d_BaseLine(:,2)]',i));
Ldist=[Ldist;x];
end
Rdist=[];
for i=2:19
eval(sprintf('x=[TP%d_BaseLine(:,4)]',i));
Rdist=[Rdist;x];
end

댓글 수: 2

I suspect Stephen is now typing a longer comment with the same message, so I'll keep it short: help yourself and don't use numbered variables or eval. It makes your code really difficult to debug (or understand). Usually a cell array or a struct is a much better idea.
Stephen23
Stephen23 2018년 9월 11일
편집: Stephen23 2018년 9월 11일
Do NOT use eval like that. Magically accessing variable names is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know why:
@Rik Wisselink: thank you for your comment. I really appreciate not being the only one who comments on these things, so please don't feel any need to keep them short!

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

 채택된 답변

Stephen23
Stephen23 2018년 9월 11일
편집: Stephen23 2018년 9월 11일
Two important changes to make:
  1. Do NOT use eval.
  2. load into an output variable.
If you had named the variables inside each .mat file with exactly the same names then your processing would be much simpler too: in contrast to what some beginners think, it is much simpler to process a sequence of .mat files if they contain exactly the same variable names.
How to improve your code depends on how many variables there are in each .mat file, and what their names are: you have not told us this, which makes it harder to help you. Assuming that each .mat file contains one variable then you can easily do something like this (untested as I don't have any sample files):
F = {'TP2_BL.mat','TP3_BL.mat','TP4_BL.mat','TP5_BL.mat','TP6_BL.mat','TP7_BL.mat','TP8_BL.mat','TP9_BL.mat','TP10_BL.mat','TP11_BL.mat','TP12_BL.mat','TP13_BL.mat','TP14_BL.mat','TP15_BL.mat','TP16_BL.mat','TP17_BL.mat','TP18_BL.mat','TP19_BL.mat'};
%S = dir('TP*_BL.mat'); % why not just use DIR ?
%F = natsortfiles({S.name}); % download from File Exchange.
C = cell(size(F));
for kk = 1:numel(F)
S = load(F{kk});
C(kk) = struct2cell(S);
end
M = vertcat(C{:});
Ldist = M(:,2);
Rdist = M(:,4);
If there are multiple variables in each .mat file, then you can select which one to import by using the regular expression syntax.

추가 답변 (1개)

Amira Rodenburg
Amira Rodenburg 2018년 9월 11일

0 개 추천

Thank you very much! I am indeed a beginner, but will keep this in mind from now on!

카테고리

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

질문:

2018년 9월 11일

답변:

2018년 9월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by