필터 지우기
필터 지우기

Is it possible to run terminal commands from matlab ??

조회 수: 41 (최근 30일)
mostafa haggag
mostafa haggag 2018년 4월 20일
댓글: Walter Roberson 2022년 7월 20일
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
mostafa haggag
mostafa haggag 2018년 4월 21일
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

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

채택된 답변

Walter Roberson
Walter Roberson 2018년 4월 20일
편집: Walter Roberson 2018년 4월 20일
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);
  댓글 수: 7
mostafa haggag
mostafa haggag 2018년 4월 21일
편집: mostafa haggag 2018년 4월 21일

so what i do in the terminal is the following ssh -X xxxxxxxx@xxxxx.xxx.xxxxxx.com (i replaced some personal data to xx in hre) then the pc asks for my password of my account so i enter it now i I am using a terminal of the other computer i change the directory of the other computer to cd palm /currentversion then i run this mrun -d example neutral -b -h xxxxx -K parallel -X 48 -T 24 -t 1800 -q xxxx.p -r "d3# ts# pr# xy#"

I wanted to know how can i add the part ssh and the password at the beginning of the code ???

sorry if i am repeating the explanation i am just trying to be as clear as possible example neutral is the name of the folder inside of the palm directory.

if I understand correctly it should be like this cmd_pattern = 'mrun -d example "%s -b -h xxxxx -K parallel -X 48 -T 24 -t 1800 -q xxxx.p -r "d3# ts# pr# xy#" > "%s" < "%s"');

also cmd_pattern = 'Mrun -d "%s" > "%s" < "%s"'); should i remove the %s for the output or dev/null will do the thing ? fprintf(fid, 'y\n'); also how does this chooses y instead of no ?? thank you sooo sooo much you are a life saver yours haggag

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

추가 답변 (1개)

Paul Shoemaker
Paul Shoemaker 2018년 4월 20일
편집: Walter Roberson 2018년 4월 20일
Have you looked at the "system" and "dos" functions in Matlab? It sounds like they might do what you need.
Paul Shoemaker
  댓글 수: 3
SHS
SHS 2022년 7월 20일
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
Walter Roberson 2022년 7월 20일
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')

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

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by