'Error when calling DOS' error

I have a tool created by using Matlab 2016, now I am using it with Matlab 2019. I got 'Error when calling DOS' error while running it. However when I go to the line where the error happens (the command for running a .bat file) and run the command manually, it works. How can I solve this problem? I copy the related part . I got the error at the last line
% run batch
if ~err
% command=['cd "' dir '" & "components.bat"'];
command=[dir(1) ': & cd "' dir '" & "' filename '.bat"'];
err=dos(command)

댓글 수: 4

Jan
Jan 2022년 2월 14일
Do you get a complete error message or is 'Error when calling DOS' the contents of the the variable err? What does "when I go to the line" exactly mean?
Cem TAGIT
Cem TAGIT 2022년 2월 14일
Hi, I attached the error window.
I try to say that I checked the line where I got the error which is the last line , err=dos(command).
the .bat file I run manually is "' filename '.bat"
Jan
Jan 2022년 2월 14일
If you use the debugger, you can step through the code line by line. I do not see, which line causes the display of the error message. Check, what the contents of err is.
Well certainly you agree that dir should not be the variable name for the folder. I suggest she try my code below (scroll down for it).
Using their code, I made a file called mybatch.bat and just put "dir" into it to get a directory listing. I tested it with this code
dir = pwd;
filename = 'myBatch';
command=[dir(1) ': & cd "' dir '" & "' filename '.bat"']
status = dos(command)
and it ran fine, putting the directory listing into MATLAB's command window. If the file is not there, I get an error but not the error they got. I'm using R2021b.
Again, I suggest you try my more robust code below, which checks to make sure that the batch file exists before calling dos().

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2022년 2월 14일

0 개 추천

dir is a built-in function name. Do NOT use it as the name of a variable. Use folder instead. Try this:
folder = 'C:\';
filename = 'myBatchFile.bat'
fullFileName = fullfile(folder, filename)
if ~isfile(fullFileName)
errorMessage = sprintf('Error: file does not exist:\n%s')
uiwait(errordlg(errorMessage));
return;
end
commandLine = sprintf('%s: & cd "%s" & "%s"\n', folder(1), folder, filename)
err = dos(command)

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2022년 2월 14일

댓글:

2022년 2월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by