Is it possible to run terminal commands from matlab ??
37 views (last 30 days)
Show older comments
mostafa haggag
on 20 Apr 2018
Commented: Walter Roberson
on 20 Jul 2022
hello I am trying to run a command in terminal using Matlab
First,I need to change directory to a specific directory first using Matlab then I need run a command this command will be repeated 200 times but with different file names.
I do not want to keep entering the same command 200 times Not to mention when I enter the command to run the simulation, the terminals reply back asking me to confirm with y or n so I need to send 'y' for (yes) character too. is such thing possible or should I do it manually ?? I hope I get some guidance on what I need to search for exactly
thank you so much haggag
1 Comment
mostafa haggag
on 21 Apr 2018
What i usually do in Ubuntu I open terminal then i write an ssh command to connect to another pc and then i am asked for my password I change my home directory to the place the program is For example Cd palm/current_version Then i write the command Mrun - d filename........ (this line of Mrun is the one that need to be repeated many times) So
Accepted Answer
Walter Roberson
on 20 Apr 2018
Edited: Walter Roberson
on 20 Apr 2018
project_dir = '....'; %the place the files are input_extension = 'dat'; %change as needed output_extension = 'out'; %change as needed
cmd_pattern = '"C:\Program Files (x15)\Plan8\frobber.exe" -i "%s" -o "%s" < "%s"');
if ~exist(project_dir, 'dir') error('Input directory does not exist, "%s"', project_dir); end
dinfo = dir( fullfile(project_dir, ['*.', input_extension] ) ); if isempty(dinfo); error('No files of appropriate type in "%s", project_dir); end file_names = {dinfo.name};
sayyes_file = [tempname() '.yes']; fid = fopen(sayyes_file, 'wt'); if fid < 0 error('Could not write temporary file: "%s"', sayyes_file); end fprintf(fid, 'y\n'); fclose(fid);
olddir= cd(project_dir);
for K = 1 : length(file_names) this_file = file_names{K}; [~, basename, ~] = fileparts(this_file); outfilename = fullfile(project_dir, [basename '.' output_extension]); cmd = sprintf(cmd_pattern, this_file, outfilename, sayyes_file); try [status, output] = system(cmd); if status ~= 0 fprintf('something went wrong processing "%s", continuing\n', this_file); end catch ME cd(olddir); error('Hard failure processing "%s", giving up', this_file); end end
cd(olddir);
More Answers (1)
Paul Shoemaker
on 20 Apr 2018
Edited: Walter Roberson
on 20 Apr 2018
Have you looked at the "system" and "dos" functions in Matlab? It sounds like they might do what you need.
Paul Shoemaker
3 Comments
SHS
on 20 Jul 2022
I am trying to run a shell from Matlab. The .sh file easily runs from the terminal, but not from Matlab. The error is:
line 9: fslroi: command not found.
It seems Matlab tries to run the whole shell file from Matlab, not from the system!
Any idea how to fix this issue?
Thanks in advance!
Walter Roberson
on 20 Jul 2022
The issue is that when you start a shell from inside a process, then by definition it is not an "interactive shell" and so your .profile or equivalent is not executed. If your shell profile adds directories to the PATH environment variable, then those directories are not going to be on the path.
There are a few different workarounds:
- add the PATH initialization to the system-wide shell profile. Traditionally that was /etc/profile but on MacOS you would instead need to edit a particular .plist
- instead of having your command be directly system('fsutil etc') you can instead system('. ~/.profile; fsutil etc') where ~/.profile is a path to the profile to be sourced
- or you could system('/path/to/fsutil etc')
- or you could system('PATH=$PATH:/path/to/; fsutil etc')
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!