Pass the content of a variable to a .bat file from matlab

5 views (last 30 days)
Sorry, I have a new problem. I have this char variable: name='file.pdf';
when I do !test name
I see in the file test.bat "name", that is the container of the variabile but not the content of the variable.....I need to pass the content name.pdf to the file test.bat.
How can I resolve?? How cas I pass the string name.pdf to the .bat?

Answers (1)

dpb
dpb on 13 Feb 2016
Edited: dpb on 14 Feb 2016
Use system instead for command form to evaluate variables in building the command string. ! ("Bang") interprets everything after it as character string so doesn't (as you've discovered) evaluate the variable before passing it to the OS.
>> f='c*.m'; % selected list of m-files in variable
>> [~,res]=system(['dir /b ' f]) % do a command; "dir" in the example...
res =
categorizeDesignated.m
currenteval.m
>>
ADDENDUM
Just do it again...
system(['report.bat ' res])
Or, more likely you'd want in real life something like
system(['report.bat <dir /b ' f])
Build the string that are going to submit just as you would any other string in Matlab; the limitation is that must(*) use the functional form instead of the bang operator to get a variable interpreted.
() without resort to evil *eval, which route I'm not going to suggest...albeit under some circumstances it is useful.
  1 Comment
luigi luigi
luigi luigi on 14 Feb 2016
thanks but now how I can pass to file report.bat the content of the variable "res"? sorry for my problems!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!