Matlab Batch Run Returns Sript-as-Function Error
이전 댓글 표시
I am attemptting to follow along on this blog post but am getting the following error below. The myscript matches exactly the one in the post. I am not really sure how the the post's matlab instance and mine are different but clearly something. Any help would be lovely.
Ultimately, I am trying to use the command below specifically because it starts an independant process and will close terminate automatically after the script runs. I need to kick off a bunch of different instances of this.
I read the documenation for 2020a, and it seems like it is doable
The errors are below:
>> "C:\Program Files\MATLAB\R2020a\bin\matlab.exe" -batch myscript
"C:\Program Files\MATLAB\R2020a\bin\matlab.exe" -batch myscript
↑
Error: Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
>> matlab -batch "myscript"
Attempt to execute SCRIPT matlab as a function:
C:\Program Files\MATLAB\R2020a\toolbox\matlab\general\matlab.m
댓글 수: 3
To just add to @Stephen23 comment -- there isn't such a thing as starting a script as a batch job if you're already in MATLAB; that only makes sense if actually at a command prompt.
What are you really trying to accomplish here with "I need to kick off a bunch of different instances of this." since a bunch of different "Hello World" processes isn't of much practical use. Are you perhaps wanting to run the same code over a bunch of files, maybe? What, specifically do you think you get by running multiple batch jobs instead of a loop at the IDE?
Or, maybe what you are really after is to spawn a separate instance from MATLAB is why were trying this inside? Context could perhaps help besides the answer to the specific question.
Richard
대략 7시간 전
답변 (2개)
Fangjun Jiang
대략 6시간 전
편집: Fangjun Jiang
대략 6시간 전
0 개 추천
"C:\Program Files\MATLAB\R2020a\bin\matlab.exe" -batch "myscript"
is run at the Windows Command Prompt, not inside MATLAB Command Window.
EDIT - Forgot the newline after each write to the file in the original -- dpb
Still guessing a little about the actual use case, but try something like
pgm='"C:\Program Files\MATLAB\R2020a\bin\matlab.exe" -batch '; % base command line
d=dir('InputFileNames*.m'); % a list of all the m-files to be run (~200)
N=numel(d); % how many there are total
nperbatch=10; % how many to put in a given queue
for i=floor(N/nperbatch) + ~(mod(N,nperbatch)==0) % for how many batches
fid=fopen('batch%02d.cmd',i,'wt'); % a batch file for the group
fprintf(fid,'@echo off\n'); % turn echoing off in the batch file
ix0=(i-1)*nperbatch; % first for the group
for j=1:nperbatch % for that many
ix=ix0+j; % the next file in order
if ix>N, continue, end % we're done, leave loop
[~,mfile]=fileparts(d(ix).name); % strip the trailing .m
cmd=[pgm fullfile(d(ix).folder,mname) '\n']; % create a specific case command
fprintf(fid,cmd); % and put it in the batch file
end
fclose(fid) % save that batch file...
end
The above will create a serices of .CMD batch files "batchNN.cmd" that will each have the command to start a MATLAB process running the given m-file, again assuming there is a series of uniquely named m-files to be run. That's about the only way I see if each case to be run is actually a script and not a function. If they were functions instead, the -batch parameter value string is passed directly to the MATLAB interpreter so whatever syntax would be required to run the function would go in place of the above m-file names.
When ready to start another group, just execute the resulting batch file from a CMD session. When that group finishes, you can then submit the next batch. Not completely automated; would need a way to monitor that the first batch are done and wait for that flag.
카테고리
도움말 센터 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!