Trouble running this script on windows, could you please tell me what i am doing wrong?

4 views (last 30 days)
the following script runs on my Mac without any problems, but when I try to run the same script on windows (I have made sure the path name and the backslash have been used correctly) it gives me the following error:
the code i am using is:
nfiles = input('Number of tissue files available: ');
for j = 1:nfiles
DataC = dlmread(sprintf('/Users/srikantasharma/Desktop/Sept-scan/Male/106904(40db)/position-%d.txt',j)); *the code for mac*
the code for windows:
nfiles = input('Number of tissue files available: ');
for j = 1:nfiles
DataC = dlmread(sprintf('D:\srikantasharma\Desktop\Sept-scan\Male\106904(40db)\position-%d.txt',j));
Error: The file D:cannot open file because: Existenece?memory? permissions?...
When I run an individual file though it seems to run! Could you please tell me what I am doing wrong?
Many thanks.
  3 Comments
Srikanta Sharma
Srikanta Sharma on 17 Jan 2013
Hello Jan, the complete error message is: The file 'D:' could not be opened because: Cannot open file. Existence? Permissions? Memory? . . .
Error in ==> automated_image at 8 DataC = dlmread(sprintf('D:\Dec-30Days-2013\30-days-SI\128765(40 db)\position-%d.txt',j));
Jan
Jan on 18 Jan 2013
SPRINTF cannot run correctly, when the format strings contains backslashes "\", because they are interpreted as escape characters. I have posted a working solution already: Use FULLFILE to join path and filename, and SPRINTF for the file name only.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Jan 2013
Change the backslashes to forward slashes. Forward slashes are acceptable to MS Windows.
The problem you have is that you have backslash sequences such as \D inside the format string for sprintf. backslashes are special in formats. For example, \t means tab and \r means carriage return.
If you prefer to write the names in terms of backslashes then recode as
DataC = dlmread(sprintf('%s%d%s','D:\srikantasharma\Desktop\Sept-scan\Male\106904(40db)\position-', j, '.txt'));

More Answers (1)

Jan
Jan on 17 Jan 2013
Edited: Jan on 17 Jan 2013
The error message is clear: Please check if the file exists:
if isunix
base = '\';
else
base = 'D:\';
end
folder = fullfile(base, 'srikantasharma\Desktop\Sept-scan\Male\106904(40db)');
for j = 1:nfiles
File = fullfile(folder, sprintf('position-%d.txt',j));
try
dataC = dlmread(File);
catch
if exist(File, 'file') ~= 2
error('Missing file: %s', File);
else
error('Cannot read existinmg file: %s', File);
end
end
end

Tags

Community Treasure Hunt

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

Start Hunting!