views:

176

answers:

2

I have run simulations on software (named CMG) using command prompt. 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 (.rwo format) containing the desired result, we have to run the executable of the report program which uses a parameter file (.rwd format). For example if I were to implement these steps in command prompt, it would be like this:

“C:\Program Files\CMG\BR\2008.10\Win32\EXE\report.exe” –f ReportBHP1.rwd –o ReportBHP1.rwo

The output file is ReportBHP1.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 call CMG for each realization and extract the data.

for nr=1:NREAL

      dlmwrite(‘PERM.INC’,PERM(:,nr),’delimiter’,’\n’); % Writes the permeability file for each realization

       system('"C:\Program Files\CMG\IMEX\2008.10\Win32\EXE\mx200810.exe" -f "C:\Documents and Settings\HSingh2\Desktop\Work\Model - SGEMS, CMG and MATLAB\ReservoirModel_CMGBuilder.dat"');    % calls CMG
       system('"C:\Program Files\CMG\BR\2008.10\Win32\EXE\report.exe" –f "C:\Documents and Settings\HSingh2\Desktop\Work\Model - SGEMS, CMG and MATLAB\ReportBHP1.rwd" –o "C:\Documents and Settings\HSingh2\Desktop\Work\Model - SGEMS, CMG and MATLAB\ReportBHP1.rwo"'); % calls report for well 1

      [TIME,BHP1]=textread(‘"C:\Documents and Settings\HSingh2\Desktop\Work\Model - SGEMS, CMG and MATLAB\ReportBHP1.rwo".rwo’,’%f\t%f’); % Reads the well data and store it in the variable BHP1 for well 1

end

So I have two problems:

1) When I run this script in Matlab, it does not generates output file ReportBHP.rwo. So consequently it gives 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 realizations to run, then for every realization I would be prompted to hit enter to proceed. The following line causes the prompt:

system('"C:\Program Files\CMG\BR\2008.10\Win32\EXE\report.exe" –f "C:\Documents and Settings\HSingh2\Desktop\Work\Model - SGEMS, CMG and MATLAB\ReportBHP1.rwd" –o "C:\Documents and Settings\HSingh2\Desktop\Work\Model - SGEMS, CMG and MATLAB\ReportBHP1.rwo"'); % Calls report

I will appreciate if someone can solve these issues which have have paused my work. Thank you.

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 realization. However the final outcome will be confirmed only when I am able to run whole of my program which involves running hundreds of realizations of model.

Update 2: I can run the software (CMG) and generate output file (.rwo format) using command-prompt as follow:

  • This command makes my CMG software run:

    “C:\Program Files\CMG\IMEX\2008.10\Win32\EXE\mx200810.exe” –f CMGfile.dat

  • This command reads the report parameter file (.rwd format) and generates output file (.rwd format):

    “C:\Program Files\CMG\BR\2008.10\Win32\EXE\report.exe” –f ReportBHP.rwd –o ReportBHP.rwo

Thanks to Jacob and yuk who have been trying to resolve the problem.

LATEST EDIT:

1) I am able to run the software, avoid the prompt to hit the return key and generate the output file (.rwo format) using Matlab through the following commands:

system('report.exe /f ReportBHP1.rwd /o ReportBHP1.rwo')
system('mx200810.exe -f ReservoirModel_CMGBulider.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:
              'ReservoirModel_CMGBuilder.dat'
              Check path name for spaces, special character or a total length greater than 256 characters

              Cannot find data file named 'ReservoirModel_CMGBuilder.dat'

Date and Time of End of Run: .....

ans = 0"

As a consequence of this the result of my all the realizations is coming out to be same as the first realization. I need help in correcting this error.

+2  A: 

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 CMGfile.dat) inside the quotes as below:

  %# Calls CMG 
  system('"C:\Program Files\CMG\IMEX\2008.10\Win32\EXE\mx200810.exe" –f CMGfile.dat'); 

  %# Calls report
  system('"C:\Program Files\CMG\BR\2008.10\Win32\EXE\report.exe" –f ReportBHP.rwd –o ReportBHP.rwo'); 

Update:

Here's a cheap trick to solve your second problem (type q to terminate the program):

  %# Calls CMG     
  system('"C:\Program Files\CMG\IMEX\2008.10\Win32\EXE\mx200810.exe" –f "C:\Some Path\CMGfile.dat" < "C:\inp.txt"'); 

  %# Calls report   
  system('"C:\Program Files\CMG\BR\2008.10\Win32\EXE\report.exe" –f "C:\Some Path\ReportBHP.rwd" –o "C:\Some Path\ReportBHP.rwo" < "C:\inp.txt"');
  1. 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.
  2. 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).
Jacob
I will update it and let you know the result by evening as currently I am on a computer which doesn't has the software. Thank you.
Harpreet
I've included a "trick" which might solve your second problem
Jacob
Please find a little change from ".." to '"..."...' above. It was my mistake, and the one above now is the correct format which I was using as I saw in my code. I tried both of your suggestions and using neither of two the output file is generated. For the first update you posted, the 2 repeated errors are "'C:\Program' is not recognized as an internal or external command, operable program or batch file". There is no simulation report generated either. For the 2nd update you posted, the 2 repeated errors are "The system cannot find the file specified."Again no simulation report is generated.
Harpreet
I've updated my answer. Try it now
Jacob
Are you sure that the locations of `"C:\Program Files\CMG\IMEX\2008.10\Win32\EXE\mx200810.exe"` and `"C:\Program Files\CMG\BR\2008.10\Win32\EXE\report.exe"` are correct?
Jacob
As the computer with the software I work on is not my personal, so I do not have authorization to store the inp.txt file in C:\inp.txt. I saved the inp.txt file in my folder I have other related files, and used that address in the code instead of C:\inp.txt. Is it ok to do that?
Harpreet
Yes, those locations are correct.
Harpreet
On running your updated first script (the top one), I get a prompt: "*** Enter file name for data file Q to quit, <C/R> to use default name of 'imex.dat':"I tried entering the file "ReservoirModel_CMGBuilder.dat" (as shown in updated question), but it says "... File 'ReservoirModel_CMGBuilder' not found, please try again ..." and then the first prompt comes again.
Harpreet
On running your second script (the bottom one), I again get the 2 repeated errors: "The system cannot find the file specified." No simulation report is generated either. I have saved the inp.txt file in a folder on desktop and I am using that location instead of C:\inp.txt
Harpreet
Have you enclosed the new path of `inp.txt` in `"` as well?
Jacob
No. It is as you have shown in the bottom script.
Harpreet
But since you've changed the path, to something with spaces in it, enclose it. E.g. `"C:\Users\Harpreet Harpreet\Desktop\inp.txt"`
Jacob
Thanks Jacob. I'll get back home in evening and access the computer through remote desktop and let you know if this works. So you mean I should change to this: system('"C:\Program Files\CMG\BR\2008.10\Win32\EXE\report.exe" –f ReportBHP.rwd –o ReportBHP.rwo < "C:\Users\Harpreet Harpreet\Desktop\inp.txt"');?
Harpreet
`"C:\Users\Harpreet Harpreet\Desktop\inp.txt"` was an example. You should replace it with the full path of `inp.txt` on your Desktop. This can be obtained by right-clicking the file and looking at its properties. The full path will be next to **Location**.
Jacob
Yes, I understand that :). What I meant to ask if I need to put < outside the inverted commas as < "..." or without it like "..."? Thanks for keeping up.
Harpreet
Yes, the `<` should be outside `"..."` since `"..."` should only enclose file paths with spaces in them.
Jacob
Jacob: After enclosing it in "..." I am only able to avoid the manual entry on prompt. No output file is developed and hence I get an error of "file not found" in line where 'textread' is used. Everything in the code is as per you showed.
Harpreet
OK, so we're getting somewhere. I've updated my answer to add a pipe out (`> "C:\inp.txt"`). This will dump any error messages to `"C:\err.txt"`. Change the path to whatever works for you.
Jacob
Also, try replacing `CMGfile.dat`,`ReportBHP.rwd` and `ReportBHP.rwo` with the full paths. i.e., `"C:\Some Path\ReportBHP.rwd"` ; maybe the output files are created but in some other location.
Jacob
Ok, I will do this and let you know (again in evening I'm sorry! :)). So the file err.txt will be automatically created to store error messages, if there are any? Also while while replacing CMGfile.dat,ReportBHP.rwd and ReportBHP.rwo with the full paths do I need to enclose them inside quotes as "...", or without quotes? Thanks so much Jacob.
Harpreet
Yes, you'll need to enclose them in quotes if the paths contain spaces just like `inp.txt`. And yes, `err.txt` will store error messages, and no problem :)
Jacob
Jacob, this step too couldn't create output file. The err.txt file is storing this: "RESULTS Report (2008.10 Jul 11 2008)Enter Report Command filename or 'q' to quit:". Due to introduction of this err.txt the report of CMG (software) which used to be visible in Matlab is no more visible. Finally due to no .rwo file the error is displayed in Matlab command window when the program reaches at 'textread'.
Harpreet
OK, sorry about that. Remove the output pipe and keep the full paths for `CMGfile.dat,ReportBHP.rwd` and `ReportBHP.rwo` --- I've updated my answer as well
Jacob
Hi Jacob: I included the path of the files and did exactly as you showed. But still the output file did not generate :(
Harpreet
Though the report of CMG issue is back to normal again i.e. report is coming on Matlab's command window.
Harpreet
Are you sure the output file is generated by you just typing `q` and hitting the return key? Also, how did you create `inp.txt`? Give me the exact details.
Jacob
I've created a txt file which has `Q` and a return key --- try this:http://download200.mediafire.com/3y00q90wtt9g/ij1m0tmijum/inp.txt
Jacob
I didn't say that output file is created by just typing q and hitting the return key. I said that report, which is different from output file, is displayed on command window when we don't use err.txt file. The err.txt file is storing this: "RESULTS Report (2008.10 Jul 11 2008) Enter Report Command filename or 'q' to quit:". The output file contains the variable we want to export, while the report is a summary of the errors or warnings etc. I hope I didn't confuse you.
Harpreet
So what's the exact sequence of operations you do? Could you list them out in your answer? As in, run `mx200810.exe`, then run `report.exe`, hit `Q`, hit return, etc. Just give it all in detail so I have the right picture.
Jacob
"Are you sure the output file is generated by you just typing q and hitting the return key?" Do you mean the input file? If you mean inp.txt file, then yes I created it by just typing q and hitting the enter key.
Harpreet
Ok, I'll write it down for you. But can you tell me how to add formatting (highlighting, bold etc) in comment? I don't see option when writing a comment.
Harpreet
I'm first running mx200810.exe, then run report.exe. I am not hitting Q and return, because its being taken care of by inp.txt file as you said. The Matlab's command-window is also perfectly showing the simulation result of the software as normal. But when the program reaches the line where 'textread' function is used, I get an error due to missing output (.row format) file.
Harpreet
So if you ran the software normally without using `inp.txt`, would the `.row` file be generated?
Jacob
Also, are you sure that the the `.row` file is **not** generated? Or is it just that `textread` is unable to find it?
Jacob
Yes. If I run the software normally without using inp.txt, still there's no generation of .rwo file (sorry, it's .rwo and not .row format)
Harpreet
It doesn't generates at all. Even if I include the path still I can't see the .rwo file. So I'm assuming that it doesn't generates.
Harpreet
Then there's nothing I can do. I was working under the assumption that you *could* generate the `.rwo` file and I tried to duplicate your actions. I suggest you look at the documentation of `report.exe` (which I think generates the required file) and see what's going on. Did you put down the **full path** of `ReportBHP.rwo` so that you know where to look?
Jacob
Yes, I did put down the full path of ReportBHP.rwo.
Harpreet
As I mentioned in my question, the output file does generates but when I run software using command-prompt. It doesn't when using Matlab. I need to use Matlab because I have hundreds of models which I need to run on software, and which is feasible through Matlab script.
Harpreet
Wait, so when running from the command prompt with the same command the output file is generated?
Jacob
If so, could you include what you typed at the command prompt which seemed to generate the output file?
Jacob
This command makes makes my CMG software run: “C:\Program Files\CMG\IMEX\2008.10\Win32\EXE\mx200810.exe” –f CMGfile.dat
Harpreet
This command generates the report and output file: “C:\Program Files\CMG\BR\2008.10\Win32\EXE\report.exe” –f ReportBHP.rwd –o ReportBHP.rwo
Harpreet
The output file is ReportBHP.rwo, that file will contain the variable exported.
Harpreet
Report parameter file is ReportBHP.rwd.
Harpreet
I've added updates to my question. See above.
Harpreet
Can you update the question with the latest version? With the full paths for the `.rwo` files, etc.?
Jacob
I've updated the code by including the location of the file. Or do you mean something else?
Harpreet
Try replacing the path in `textread` with the full path, i.e. `"C:\Documents and Settings\HSingh2\Desktop\Work\Model - SGEMS, CMG and MATLAB\ReportBHP1.rwo"`
Jacob
Did that. But if the output file is not created prior to 'textread' then what difference it can make?
Harpreet
And you're positive `"C:\Documents and Settings\HSingh2\Desktop\Work\Model - SGEMS, CMG and MATLAB\ReportBHP1.rwd"` exists?
Jacob
Yes. It's a standard parameter file. It is pre-made and we can make some changes to suit our needs of parameters we want to export in form of output file. The format (not .rwd) of this file is standard as pre-defined in Software manual help.
Harpreet
Hi Jacob: Please see my latest update above in the question. That issue is sorted but there's another problem.
Harpreet
+1  A: 

I answered to a duplicate of this question first. That one should be deleted, but here is my two cents from that answer for the first part.

Use both outputs to get status of system run and text result, if any will be available.

cmd_line = '“C:\Program Files\CMG\BR\2008.10\Win32\EXE\report.exe” –f ReportBHP.rwd –o ReportBHP.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
[TIME,BHP]=textread(...

If your parameters are variable you can generate the command line string in MATLAB using string concatenation or SPRINTF function.

yuk
That post is deleted yuk. Thanks for your answer and I will let you know if this works well :)
Harpreet
**"??? Error using ==> systemArgument must contain a string."** This is the error in line [status, result] = system(cmd_line);
Harpreet
If I use [status, result] = system('cmd_line'), then the above error is gone, but there's "Error running report.exe" error.
Harpreet
This is the value of the 'result' variable "result ='cmd_line' is not recognized as an internal or external command,operable program or batch file."
Harpreet
cmd_line is MATLAB variable. Do no put it in quotes. Use `[status, result] = system(cmd_line)`.
yuk
Did you assign a command string to `cmd_line` before calling `system`?
yuk
I did exactly the same as you suggested. I got an error **"??? Error using ==> system Argument must contain a string."**. On getting this error I put cmd_line in '...'. Before calling system I did assign cmd_line as cmd_line = '“C:\Program Files\CMG\BR\2008.10\Win32\EXE\report.exe” –f ReportBHP.rwd –o ReportBHP.rwo';
Harpreet
This line [status, result] = system(cmd_line); gave the error of "??? Error using ==> system Argument must contain a string."
Harpreet
This is strange. Try to run `class(cmd_line)`, will it return `char`? Try to put your command string directly to system call as the argument.
yuk
I will try it in evening when I have access to computer and software both. Will let you know the result. Thanks.
Harpreet
I am sorry. I missed out a line for calling CMG in my try on your initial suggestion. I added that line and tried again. Then tried your second suggestion of class(cmd_line). However both of your suggestion give same result, i.e. they stop inside the for loop and give the error "Error running report.exe" in Matlab's command window. Continued below...
Harpreet
Coming to the result of variables of first suggestion: "result=The filename, directory name, or volume label syntax is incorrect" and "status=1". Coming to the result of variable of the second suggestion: "status='char'" So regarding your question whether class(cmd_line) returns 'char' - Yes it does returns 'char' as you can see what I wrote. While running class as per your second suggestion there should be only one output argument, so I took that output argument as "status" which is storing 'char'. Continued below...
Harpreet
As I told in beginning of third last comment that both suggestions result in stopping of program inside the for loop with display of that error on command window. Output file is not generated from either suggestion.
Harpreet
What is you OS?
yuk
Are you sure you can run that command outside of MATLAB? From command prompt?
yuk
Yes. I have run that using command-prompt and the output file also gets generated when using command prompt.
Harpreet
My Operating System is Windows XP.
Harpreet
Will your program work correctly, if you run it from command prompt from the folder, which is current in MATLAB? You can find the folder running `pwd` in MATLAB. May be you should first change the workin directory to where your input files located with `cd dirname`. Or include full path of input files into command line.
yuk
"Will your program work correctly, if you run it from command prompt from the folder, which is current in MATLAB?" - Yes it does."Or include full path of input files into command line." - I've done that too as it was suggested earlier by Jacob.
Harpreet
What about if you try to call from MATLAB some other program? Something general like Wordpad: `"c:\Program Files\Windows NT\Accessories\wordpad.exe" somefilename.txt`. First try to make it work from command prompt. Then through system in MATLAB. Let us know if it works, and if not, what will be the error messages.
yuk
Try also to use exclamation point (!, http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/f0-12994.html#f0-38522) to run external program from MATLAB. It 's easier then `system`. Everything you enter after ! symbol passed to OS. Just type `!“C:\Program Files\CMG\BR\2008.10\Win32\EXE\report.exe” –f ReportBHP.rwd –o ReportBHP.rwo`.
yuk
Try it also with the wordpad test.
yuk
"c:\Program Files\Windows NT\Accessories\wordpad.exe" somefilename.txt - Its not working even in command-prompt.
Harpreet
This is just an example. I gave the path how it's on my computer. Yours might be different and wordpad may not be installed. Just to try something that should work for sure.
yuk
I gave the path which is same as yours, however there is no wordpad executable. There is wordpad application file.
Harpreet
My path is also same as yours. But there is no executable in that folder.
Harpreet
How long does it usually take for the programs to generate the output files? If you just run mx200810 from MATLAB, may be it will pass the control back to MATLAB, but the output will be generated later? Can you check that?
yuk
When I use command-prompt it takes seconds to generate the output file.
Harpreet
!“C:\Program Files\CMG\BR\2008.10\Win32\EXE\report.exe” –f ReportBHP.rwd –o ReportBHP.rwo - This works the same way as 'system'. No output file still.
Harpreet
Hi yuk: Please see my latest update above in the question. That issue is sorted but there's another problem.
Harpreet