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

4 次查看(过去 30 天)
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 个评论
Srikanta Sharma
Srikanta Sharma 2013-1-17
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 2013-1-18
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.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2013-1-17
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'));

更多回答(1 个)

Jan
Jan 2013-1-17
编辑:Jan 2013-1-17
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

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by