Index exceeds array bounds error when trying to reach .tif images from file

2 ビュー (過去 30 日間)
Brendan Wolan
Brendan Wolan 2018 年 11 月 15 日
コメント済み: Brendan Wolan 2018 年 11 月 23 日
I'm working on a program that tracks the movement of particles across many images and as such, it needs to access the file where these images are stored. However, I continue to run into the following issue when trying to access the images:
srcFiles =
0×1 empty struct array with fields:
name
folder
date
bytes
isdir
datenum
Index exceeds array bounds.
Error in Particletracking (line 16)
filename = strcat('/Users/andromache/Desktop/Research/data/other/180319_114103_A1\ 20X\ PL\ FL\
Phase\ \(3\)',srcFiles(i).name);%Change filepath here
This corresponds to the following code:
%% Read File, filter signal, localize and track particles
srcFiles=dir('/Users/andromache/Desktop/Research/data/other/180319_114103_A1\ 20X\ PL\ FL\ Phase\ \(3\)*.tif ') %Change filepath here
pos_list=[];
dt=0.15;
nbimage=180;
for i=1:nbimage
%Analyze picture n°a to b
i;
filename = strcat('/Users/andromache/Desktop/Research/data/other/180319_114103_A1\ 20X\ PL\ FL\ Phase\ \(3\)',srcFiles(i).name);%Change filepath here
a = imread(filename);
a=2^16-a;
%BandPass
b = bpass(a,1,10);
%Findparticles
pk = pkfnd(b,5000,11);
cnt = cntrd(b,pk,15);
%Create list of positions
if find(cnt) ~=0
pos_list=[pos_list; cnt(:,1),cnt(:,2), ones(length(cnt(:,1)),1).*i*dt];
end
end
Solutions attempted include running the code on both mac and windows as well as moving the filepath, and adjusting the filepath ad nauseum. I've inherited this project and code from a growing line who have successively added to the project, so I don't have a full understanding of the code and, given that I can't run it, am haivng a tough time trying to build an understanding of it.
Any suggestions are appreciated.
  2 件のコメント
Bob Thompson
Bob Thompson 2018 年 11 月 15 日
The error is occurring because srcFiles is empty. I suspect there is an issue with your file path, as you have some folders separated with / and some separated with \.
Brendan Wolan
Brendan Wolan 2018 年 11 月 16 日
This is the directory copied directly from my terminal:
/Users/andromache/Desktop/Research/data/other/180319_114103_A1\ 20X\ PL\ FL\ Phase\ \(3\)

サインインしてコメントする。

採用された回答

Ken Atwell
Ken Atwell 2018 年 11 月 21 日
編集済み: Ken Atwell 2018 年 11 月 21 日
Those backslashes are likely not really part of the folder name, but inserted by the Terminal program to escape spaces in the folder name. As others have suggest, renaming the folder to something simplier is the easiest way forward.
Or, try this: Replace your use of dir with:
srcFiles=dir('/Users/
This press the Tab key. Use tab-completion to drill down to the desired folder, then close the string.
I suspect you'll send up with an assignment like this (some line wrapping inserted by MATLAB Answers):
srcFiles=dir('/Users/andromache/Desktop/Research/data/other/180319_114103_A1 20X PL FL
Phase (3)');
Later, don't use strcat, but fullfile to create the filename:
filename = fullfile(srcFiles(i).folder, srcFiles(i).name)
Hope that helps
  1 件のコメント
Brendan Wolan
Brendan Wolan 2018 年 11 月 23 日
The simpler filepath solved the issue. Thanks all for the suggestions and help.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2018 年 11 月 16 日
編集済み: Image Analyst 2018 年 11 月 16 日
Try making it more robust to discover what the problem is:
folder = 'C:/Users/andromache/Desktop/Research/data/other/180319_114103_A1\ 20X\ PL\ FL\ Phase\ \(3\)'
if ~exist(folder, 'dir')
message = sprintf('Directory does not exist:\n%s', folder)
uiwait(errordlg(message));
return;
end
filePattern = fullfile(folder, '*.tif')
sourceFiles = dir(filePattern)
if isempty(sourceFiles)
message = sprintf('No TIF files exist in folder:\n%s', folder);
uiwait(errordlg(message));
return;
end
for k = 1 : length(sourceFiles)
fullFileName = fullfile(folder, sourceFiles(k).name); % Change filepath here
fprintf('Found TIF file: %s\n', fullFileName);
end
  2 件のコメント
Brendan Wolan
Brendan Wolan 2018 年 11 月 19 日
It exits with the message
srcFiles =
0×1 empty struct array with fields:
name
folder
date
bytes
isdir
datenum
folder =
'/Users/andromache/Desktop/Research/data/other/180319_114103_A1\ 20X\ PL\ FL\ Phase\ \(3\)'
message =
'Directory does not exist:
/Users/andromache/Desktop/Research/data/other/180319_114103_A1\ 20X\ PL\ FL\ Phase\ \(3\)'
Clearly the code isn't finding the directory, however the above directory is a direct copy of the directory from the terminal on my mac. Is there a better way to find the file directory?
Image Analyst
Image Analyst 2018 年 11 月 19 日
I don't know - I don't use a Mac. Perhaps move the files to a folder that doesn't have so many spaces, parentheses, and slashes. I've added the tag "mac" to your post so maybe some mac people will answer. Or else call tech support.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by