How to run(scriptname) with string argument?

19 views (last 30 days)
ellun axam
ellun axam on 13 Jul 2020
Answered: Steven Lord on 6 Oct 2021
Could anyone please inform how to run a script at different path with a string argument?
function abc(fname)
if (~isstring(fname) && ~ischar(fname))
throw(MException('abc:WrongArgument', '''fname'' should be a string, wrong input: %s', num2str(fname)));
end
if (~isfile(fname))
throw(MException('abc:FileDoesNotExist', 'File %s not found', fname));
end
end
Can run with a integer argument as follows -
>> run('/home/uname/tmp/abc(2)')
Error using abc (line 3)
'fname' should be a string, wrong input: 2
Error in run (line 91)
evalin('caller', strcat(script, ';'));
String attemps:
>> run('/home/uname/tmp/abc(2s)')
Error using run (line 55)
Error: Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of
parentheses.
>> run('/home/uname/tmp/abc(\'2s\')')
run('/home/uname/tmp/abc(\'2s\')')
Error: Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of
parentheses.

Answers (2)

Malcolm Wildgoose
Malcolm Wildgoose on 6 Oct 2021
Due to the way the run function is written, it will not allow you to pass in a char array.
The work around I've found is to use strings (double quotes) instead.
run('/home/uname/tmp/abc("2s")')
If you are using a function that definitely requires chars not strings, you can use the char function:
run('/home/uname/tmp/abc(char("2s"))')

Steven Lord
Steven Lord on 6 Oct 2021
The run function is intended to run script files, not function files. Scripts do not accept input arguments.
If you want to run a function file in another directory that is not on the MATLAB search path, to be exception safe:
% Show that I'm not in c:\temp
>> pwd
ans =
'C:\Program Files\MATLAB\R2021a'
% Show the file I want to run
>> type c:\temp\example564467.m
function y = example564467(x)
y = x.^3+2*x.^3+3*x-4;
% Store the current directory and set up an onCleanup object to go back
% there when it is deleted
>> P = pwd;
>> goback = onCleanup(@() cd(P));
% CD to the location of the function to execute
>> cd c:\temp
% Call the function
>> example564467(1:5)
ans =
2 26 86 200 386
% Go back to where I started
>> delete(goback)
>> pwd
ans =
'C:\Program Files\MATLAB\R2021a'
If this were in a function, regardless of whether the call to example564467 succeeded or threw an error, the object goback would be deleted when the function exited and so MATLAB would return to its previous location.
Alternately you could add the directory containing the function to be executed to the MATLAB search path, though I'd probably only do that if I were going to call functions in that directory repeatedly.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!