Passing output-values from a C-program to MATLAB (via batch-file) - matlab

I have a DOS batch-file MYDOS.BAT containing:
My C-application (myApp.exe) reading an input-file (inputFile) from the DOS-command line
myApp.exe analyses inputFile and exits/returns with a code=N
How can I pass value N into my MATLAB script?
E.g: MYDOS.BAT is run by DOS>MYDOS inputFile and contains the following lines:
myApp %1
echo %ERRORLEVEL%
set samplerate=%ERRORLEVEL%
echo %samplerate%
...
C:/... matlab mymatlab.m ...
HOW CAN I THEN PASS the value %samplerate% into my mymatlab.m script?

You can use the system command to use DOS commands in Matlab. Use doc system to see the documentation.
System can have 2 outputs. The first one is a status, which will tell you if the operation succeeded. The second one is the output to the command prompt. You can parse this to get your value. You could use the following code as a guide for your situation:
[status,cmdout]=system('MYDOS.bat');
cmdout will contain the string that you are echoing to the command prompt.

Related

Read Arguments From CMD and Concatenate Strings in Bat File

I have the following code in my bat file which saved as MyLM.bat:
#echo off
matlab -automation -r "addpath('C:\Users\mojtaba\BrainModel');AddPathes;MyLM('MT5Test_LM')" > matlab_output.log
exit
In which i simply add the main path and then necessary paths and then i run my function (Which is MyLM). I am running the following code from my matlab command prompt :
!start "MATLAB test" /Min /B MyLM.bat
and it works fine and i am happy! So i can run different instances of matlab separately using different bat files. What will makes me happier is that i can pass my argument (which is 'MT5Test_LM') from matlab command prompt. So i dont need to save different bat files. What i actually need is to have some code like this :
!start "MATLAB test" /Min /B MyLM.bat 'MT5Test_LM'
then i need some piece of codes in my bat file to read this argument and concatenate some strings.
Is there any suggestion?
Have you tried using the input argument of the batch file (%1)?
See, e.g., this manual on batch file input argument.
You might want your bathc file to look like
matlab -r "myLM( %1 )"

How can I save an interaction with a command line program to a file?

I need to create output files that include the input I'm providing. For example, a run of the program might look like:
Input command: do_things
Things have been done.
Input command: stop_doing_things
Things are no longer being done.
Where "do_things" and "stop_doing_things" are input from the user.
How can I output all of the above to a file using command prompt functions?
It's not clear what environment using "script " command linux will open a new shell and save everything done it to
This works for you, if you run it at last...
CMD > D:\mycmdout.txt
In that case, maybe you can capture your input as a variable. Echo the variable into the >> mycmdout.txt, then procede with the actual commands, again piped into >> mycmdout.txt as Sunny suggested.
SET /P variable=EnterInputHere
echo %variable% >> mycmdout.txt
EDIT: Be sure to use double >> as to append result to file.

Perl script call from Windows Batch file

I have a perl script which needs to be called from Windows batch file. Batch file script reads text file and pass the parameters to perl script. Now one of the argument is "User Input". Now if i give double quotes in text file the following error comes
Could not open : No such file or directory at perl_script.pl at Line 10
code is (echo the input and its showing as User Input on command prompt)
echo %INPUT%
perl perl_script.pl %INPUT%
Now if i give single quotes then the perl_script gets only User (Input is truncated)
Please suggest the best way to handle this
echo %INPUT%
perl perl_script.pl "%INPUT%"

Calling a software from Matlab

The command prompt works well in all respect of running the software as well as generating reports and output files. To generate an ouput file containing the desired result, we have to run the executable of the report program which uses a parameter file. For example if I were to implement these steps in command prompt, it would be like this:
“path\report.exe” –f Report.rwd –o Report.rwo
The output file is Report.rwo, this file will contain the variable exported.
Now to implement this in Matlab, below is a small script giving a gist of what I am trying to achieve. It calls the software for each run and extracts the data.
for nr=1:NREAL
dlmwrite(‘file.INC’,file(:,nr),’delimiter’,’\n’); % Writes the data file for each run
system('"path\file.dat"'); % calls software
system('"path\Report.rwd" –o "path\Report.rwo"'); % calls report
[a,b]=textread(‘"path\Report.rwo".rwo’,’%f\t%f’); % Reads the data and store it in the variable b
end
So I have two problems:
1) When I run this script in Matlab, it does not generate output file Report.rwo. Consequently, it gives an error when it reaches the line containing 'textread' function because of absence of the file.
2) Everytime Matlab calls a report (.rwd file), it prompts me to hit enter or type 'q' to quit. If suppose there are hundreds of files to run, then for every file I would be prompted to hit enter to proceed. The following line causes the prompt:
system('"path\Report.rwd" –o "path\Report.rwo"'); % Calls report
OLDER EDIT: There are 2 updates to my problem as follow:
Update 1: It seems that part 2 of my problem above has been resolved by Jacob. It is working fine for one run. However the final outcome will be confirmed only when I am able to run whole of my program which involves running hundreds of files.
Update 2: I can run the software and generate output file using command-prompt as follow:
**“path\mx200810.exe” –f file.dat**
This command reads the report parameter file and generates output file:
“path\report.exe” –f Report.rwd –o Report.rwo
LATEST EDIT:
1) I am able to run the software, avoid the prompt to hit the return key and generate the output file using Matlab through the following commands:
system('report.exe /f Report.rwd /o Report.rwo')
system('mx200810.exe -f file.dat')
However, I was able to do it only after copying my required .exe and .dll files in the same folder where I have my .dat file. So I am running the .m file through the same folder where I have all these files.
2) However there is still one error in Matlab's command window which says this:
"...STOP: Unable to open the following file as data file:
'file.dat'
Check path name for spaces, special character or a total length greater than 256 characters
Cannot find data file named 'file.dat'
Date and Time of End of Run: .....
ans = 0"
Strings enclosed in " .. " are invalid in MATLAB so I do not know how your system functions can even function.
Replace all " with ' and then update your question and include the command line arguments (e.g.-f file.dat) inside the quotes as below:
%# Calls software
system('"path\mx200810.exe" –f file.dat');
%# Calls report
system('"path\report.exe" –f Report.rwd –o Report.rwo');
Update:
Here's a cheap trick to solve your second problem (type q to terminate the program):
%# Calls software
system('"path\mx200810.exe" –f "path\file.dat" < "C:\inp.txt"');
%# Calls report
system('"path\report.exe" –f "path\Report.rwd" –o "path\Report.rwo" < "C:\inp.txt"');
Create a file (e.g. C:\inp.txt) which contains the letter q followed by the return character. You can create this by opening Notepad, typing q, hitting the return key and saving it as C:\inp.txt. This will serve as the "input" report.exe seems to need.
Change all the system calls in your code so that the input from the text file we just made is piped into it. I've included the modified calls above (scroll to the end to see the difference).
Use both outputs to get status of system run and text result, if any will be available.
cmd_line = '“path\report.exe” –f Report.rwd –o Report.rwo';
[status, result] = system(cmd_line);
Continue your script depending on status variable. Stop if it over then zero.
if (status)
error('Error running report.exe')
end
[a,b]=textread(...
If your parameters are variable you can generate the command line string in MATLAB using string concatenation or SPRINTF function.

run Matlab in batch mode

It seems to me that there are two ways to run Matlab in batch mode:
the first one:
unset DISPLAY
matlab > matlab.out 2>&1 << EOF
plot(1:10)
print file
exit
EOF
The second one uses option "-r MATLAB_command":
matlab -nojvm -nosplash -r MyCommand
Are these two equivalent?
What does "<< EOF" and the last "EOF" mean in the first method?
Thanks and regards!
The first method simply redirects the standard output > matlab.out and the standard error 2>&1 to the file matlab.out.
Then it uses the heredoc way of passing input to MATLAB (this is not specific to MATLAB, it is a method of passing multiple lines as input to command line programs in general).
The syntax is << followed by an unique identifier, then your text, finally the unique id to finish.
You can try this on the shell:
cat << END
some
text
multiple lines
END
The second method of using the -r option starts MATLAB and execute the statement passed immediately. It could be some commands or the name of a script or function found on the path.
It is equivalent to doing something like:
python -c "print 'hello world'"
Refer to this page for a list of the other start options.