MATLAB: File names with spaces used as command line arguments

filesystem

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.

Best Answer

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