Cannot find file that exists for second variable

2 views (last 30 days)
Ok so I have the following flow_preprocess function:
function flow_preprocess(timeseries,T1w)
%%
% FORMAT flow_preprocess(timeseries,T1w))
%
% INPUTS the names of the timeseries, T1w
% OUTPUTS the names of the newfiles created
%
if nargin < 2
fprintf('nargin is less than 2\n');
error('The minimal pipeline includes a time series and a T1w image.');
else
if ~exist('timeseries','file')
fprintf('Cannot find the file: %s\n', timeseries);
error('Cannot find %s', timeseries);
end
if ~exist('T1w','file')
fprintf('Cannot find the file: %s\n', T1w);
error('Cannot find %s', T1w);
end
end
end
and I am getting a very simple error where it is exiting at the following:
if ~exist('T1w','file')
fprintf('Cannot find the file: %s\n', T1w);
error('Cannot find %s', T1w);
end
Producting the following error:
Cannot find the file: /path/to/subject/sub-02_file1.nii.gz
However, when I pass the same file as 'timeseries', I do not get the error. Additionally, I can 'ls' the file, and the folder has been added to my path.
It is such an odd bug, does any one have any suggestions of what could be causing matlab not being able to recognize the file that exists and it on the path?
Thanks a ton!

Accepted Answer

Stephen23
Stephen23 on 9 Mar 2024
Edited: Stephen23 on 10 Mar 2024
Your code is looking for a file literally named TIMESERIES:
if ~exist('timeseries','file')
whereas what you want to do is use the variable TIMESERIES like this:
if ~exist(timeseries,'file')
Printing the content of TIMESERIES in the error message misleads you into thinking that is what EXIST has just looked for... but it isn't. Tip: use ASSERT instead of IF and ERROR:
assert(exist(timeseries,'file'), 'Cannot find the file: %s', T1w)

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!