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