File names with spaces used as command line arguments

조회 수: 34 (최근 30일)
Andrew Newell
Andrew Newell 2014년 7월 25일
댓글: Andrew Newell 2014년 7월 30일
I am trying to run an external program with command line arguments. The syntax is like this:
bertini <inputFile> <startpointFile>
The problem is, if one of the files in the command line has a space in its name, I can't get it to work. For example, if commandStr = 'bertini paramotopy.input /Users/my folder/start', then
[status,result] = system(str)
returns an error because bertini thinks the startpoint file is /Users/my.
I have not been able to make any of the usual suggestions involving backslashes or double quotes work for this example. I need it to work for both Windows and Unix-based systems.
Comment added: Bertini is not a Matlab executable; it is an entirely separate product that was written in C. Unfortunately, I don't have the option of altering its code. Sorry for the misunderstanding - it didn't occur to me that people would assume this.

채택된 답변

Image Analyst
Image Analyst 2014년 7월 25일
Try this, where I was trying to accept a foldername, that contained images, via the command line of a compiled executable:
if isdeployed && ~isempty(varargin)
% For some reason, it won't take the command line as a single item, even if it's in double quotes.
% It splits it up and gets rid of leading and trailing spaces so that
% "D:\word1 word2\theUser\Images"
% becomes 2 cells:
% varargin{1} = "D:\word1
% varargin{2} = word2\theUser\Images"
% We need to join all these together.
numberOfCells = length(varargin);
if numberOfCells >= 1
commandLineArgument = varargin{1};
for k = 2 : numberOfCells
thisWord = varargin{k};
% fprintf(1, 'varargin{%d} = %s\n', k, thisWord);
commandLineArgument = sprintf('%s %s', commandLineArgument, thisWord);
end
% Get rid of any quotes.
commandLineArgument = strrep(commandLineArgument, '"', []);
fprintf(1, 'The argument passed in on the command line is %s\n', commandLineArgument);
% msgboxw(['The argument passed in on the command line is ', commandLineArgument]);
if exist(commandLineArgument, 'dir')
% If this folder exists, use it.
handles.imageFolder = commandLineArgument;
fprintf(1, 'Changed image folder to this: %s\n', handles.imageFolder);
else
fprintf(1, 'This folder does not exist, so we cannot change to it:\n %s\n', commandLineArgument);
end
end
fprintf(1, 'handles.imageFolder = %s\n', handles.imageFolder);
end
  댓글 수: 1
Andrew Newell
Andrew Newell 2014년 7월 30일
Thanks to everyone for their suggestions. It appears that the issue is with how Bertini interprets command line arguments, so I have re-designed my program to get around that. Image Analyst, I'm accepting your answer because it might be useful to someone who is creating a stand-alone program using MATLAB.

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

추가 답변 (3개)

Robert Cumming
Robert Cumming 2014년 7월 25일
편집: Robert Cumming 2014년 7월 26일
try putting " around the command,
commandStr = '"bertini paramotopy.input /Users/my folder/start"'
edit
So the problem is running the bertin code - I would first try to get that working on its own from the command line the way you want it to (it might not be possible...) then transfer that methodology into the system command.
The input parser for your c code is space delimited - so it will be interpreting your 2 commands as 3 (since the 2nd has a space in it...).
  댓글 수: 2
Andrew Newell
Andrew Newell 2014년 7월 25일
Nope. That is one of the usual suggestions that I mentioned. It works fine if I am trying to run a command like
/Users/my folder/bertini
but not for the command line arguments.
Robert Cumming
Robert Cumming 2014년 7월 25일
ah sorry misunderstood. try single quotes or can you pass a file in. Ultimately this is an issue with bertini api and not really matlab

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


Sean de Wolski
Sean de Wolski 2014년 7월 25일
Perhaps try the ! operator:
!bertini paramotopy.input /Users/my folder/start
  댓글 수: 2
Andrew Newell
Andrew Newell 2014년 7월 25일
No difference, I'm afraid.
Image Analyst
Image Analyst 2014년 7월 25일
편집: Image Analyst 2014년 7월 25일
Did you try what I showed you? Assuming "bertini" is the MATLAB-code executable that you compiled into a standalone executable, and are trying to pass in arguments for, I know for a fact that this will work. I've been through what you're going through and basically I had to "rebuild" the command line from the parts. I know it works because I use it in my systems all the time.

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


per isakson
per isakson 2014년 7월 25일
This should work at least on Windows
commandStr = 'bertini paramotopy.input "/Users/my folder/start"';
[status,result] = system( commandStr );
if not what does result say?
  댓글 수: 5
per isakson
per isakson 2014년 7월 26일
편집: per isakson 2014년 7월 26일
I would try
bertini paramotopy.input "/Users/my folder/start"
at Windows Command Prompt (the DOS window).
Image Analyst
Image Analyst 2014년 7월 26일
Good suggestion. If it fails, then we know it has nothing to do with MATLAB and it's all bertini's fault.

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

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by