External program is not creating output
3 次查看(过去 30 天)
显示 更早的评论
Hello,
I am using Matlab R2013a and I want to run an external interactive program as non-interactive from within a matlab-script by piping the Input either through echoing a string or through a text-file. The Input consists of a string and 2 numbers. If I call
echo -e " 'String' \n 1 \n 2.2" | myFotran
in the unix shell, it creates an Output file as expected. Doing the same in matlab with
unix('echo -e " ''String'' \n 1 \n 2.2" | myFotran')
is not generating the Output-file. Why? Can someone help me? I can't change the external program, since it is not mine.
29 个评论
Rik
2017-5-23
I have next to no experience with running commandline programs from Matlab, but I generally use system to do that. Confusingly enough, there seems to be no difference between system, dos and unix.
I see no reason why your code shouldn't work, so I'd be interested in the solution as well.
Jan
2017-5-23
Are you sure that it is not generating an output file? Perhaps it is created in an unexpected location? Then providing an absolute path might help.
Matthias Otten
2017-5-23
编辑:Matthias Otten
2017-5-23
@Rik Wisselink: That's why I am so perplexed myself. @Jan Simon: I checked every Folder in my path as well as the Desktop - no Output-file created. I navigate with the Shell into my Project Folder and there I start matlab. It is also where my matlab and external script is saved. I don't know why matlab/myFortran is behaving that way. If I run the fortran program while in the Directory it creates the Output file just fine.
Jan
2017-5-23
@Matthias: I'm not sure, what the current folder is, when you run the unix command. So please try to provide an absolute path instead of searching in the paths you are expecting.
Walter Roberson
2017-5-23
Please cross-check that you are getting the same shell at the command line as you are getting through unix():
echo "$SHELL"
I am wondering whether perhaps you are hitting a situation where the echo you are reaching through unix() is a version that does not support the -e option.
Matthias Otten
2017-5-24
编辑:Matthias Otten
2017-5-24
running
echo $SHELL
in the unix Shell yields /bin/bash and so does
unix('echo $SHELL ; command')
with command being 'echo -e " ''String'' \n 1 \n 2.2" | myFotran'. It returns a 0 for properly executed command as well.
@Walter Roberson: I tried it with an input-file that works in the Linux Shell, but it also does not create an Output-file when started from within Matlab, although Matlab testifies it worked ( 0 Output).
Walter Roberson
2017-5-24
Matthias Otten
2017-5-24
@Walter Roberson: Unfortunately I don't know how the external program was compiled. I read and implemented the presented solution in the linked thread, but to no avail. The Output File was not created.
Jan
2017-5-24
@Matthias: Did you try to provide an absolut path name for the file?
I do not understand:
I tried it with an input-file that works in the Linux Shell,
but it also does not create an Output-file when started from
within Matlab, although Matlab testifies it worked ( 0 Output).
As far as I understand the shown command pipes the 'String' and the 2 numbers to the command myFotran. But where do you expect the output file to be created?
Matthias Otten
2017-5-31
After the holiday interruption, I tried providing a path
system('/path/.../myfortran < Input.txt')
Now an error occurs within myfortran, because it does not receive the Input at all. Using that exact same line in the Shell works. @Walter: Trying
System('#!bash/path/.../myfortran < Input.txt')
yielded only my original result (Seems to be working, but no Output). How do I tell Matlab to operate & save the Output in that /path/.../ Folder?
Matthias Otten
2017-5-31
I also tried
unix('/bin/bash','-echo')
/path/.../myfortran < Input.txt
but here also, myfortran is not getting any Input
Jan
2017-5-31
Are you aware that the original question contains "myFotran" repeatedly instead of "myFortran"? Note that Matlab is case-sensitive, such that "System(..." must fail. This is not nitpicking, but such typos in the code could let you think, that the code fails. Unix is case sensitive also, and the "myFotran", "myfortran" and "myFortran" are different things.
If an error occurres, please post a copy of the message. Do not let us guess, what happens.
You still do not get the point I meant:
system('/path/.../myFortran < /path/to/your/folder/Input.txt')
"Input.txt" is searched in the current folder and I'm not sure if the current folder is the same what you run system() or when you open a command shell and run the code there.
Matthias Otten
2017-6-1
编辑:Matthias Otten
2017-6-1
I am aware that it is case sensitive, but I went back and double-checked. I'll provide an example of the error: The path of Matlab as given above the Matlab Command Window is /path1/XYZ. There I do the following
unix('/bin/bash','-echo')
path1/XYZ$ /path2/XYZ/myfortran < /path1/XYZ/Input.txt
At line 123 of file myfortran.f
Fortran runtime error: End of file
When I was experiencing in the Unix-Shell how to pipe the Input into my program I got the same error, but with the command in my first post it works just fine. I am fairly new to running anything out of Matlab, I usually write and use scripts & functions within matlab.
Is it possible that Matlab blocks the Information piped into the program myfortran?
Walter Roberson
2017-6-1
Does the line
path1/XYZ$ /path2/XYZ/myfortran < /path1/XYZ/Input.txt
indicate that you invoked an interactive shell from MATLAB, and in that shell, your prompt was path1/XYZ$ and you typed in the command
/path2/XYZ/myfortran < /path1/XYZ/Input.txt
?
Walter Roberson
2017-6-1
And is it correct that if you do that
/path2/XYZ/myfortran < /path1/XYZ/Input.txt
in a shell outside MATLAB, that the command works?
I am confused as to whether the problem is occurring on MS Windows or on Linux ?
Matthias Otten
2017-6-1
What happens in "line 123 of file myfortran.f"?
I do not have access to the code, but it expects a string there, for example 'String', to name the project-file. Some lines later it expects a 1, 2 or 3 to calculate a certain szenario
Jan
2017-6-1
You do not know the code, but you know, what happens some lines later. "It expects a string there", but the message is "End of file".
Do I understand correctly that the currently discussed problem does not concern the original question "program is not creating output" anymore, but meanwhile the input is not working?
Matthias Otten
2017-6-1
Yes AND no. Apparently the input never worked in the beginning. Running it as described at the very top, yields no error, biut also no output. Providing absolute paths for program and input, as suggested, yields errors. Apparently here, matlab is not piping the input. Should I open a new question or rename this then?
Walter Roberson
2017-6-1
I still think it might be to do with the units. See https://www.mathworks.com/matlabcentral/answers/44388-or-system-or-unix-input-redirection
Matthias Otten
2017-6-6
@Walter Roberson: it works with the bash Operator now, but it did not with unix() or System(); Thanks!
Walter Roberson
2017-6-6
Did you find a way to get it to work from inside MATLAB? If so please post what you found.
Matthias Otten
2017-6-6
the following did it for me. Setting STDOUT was not necessary
setenv('GFORTRAN_STDIN_UNIT', '5')
setenv('GFORTRAN_STDOUT_UNIT', '6')
setenv('GFORTRAN_STDERR_UNIT', '0')
!myfortran < Input.txt
setenv('GFORTRAN_STDIN_UNIT', '-1')
setenv('GFORTRAN_STDOUT_UNIT', '-1')
setenv('GFORTRAN_STDERR_UNIT', '-1')
The next step is to somehow "suppress" the graphic Output, so I can iterate
Matthias Otten
2017-6-7
I tried it back then, but only with echoing the Information and I re-tried it now. Echoing does not work, input-file does. I am sorry that I caused you unnecessary trouble
Jan
2017-6-7
If you found a solution finally, the discussion was necessary and successful. It is the nature of questions, that a solution is sometimes not efficient.
Please post your solution as an answer and accept it. Thanks.
Walter Roberson
2017-6-8
The shell echo does not use the fortran unit numbers, so I would not have expected the behavior to change for an echo test.
采纳的回答
Matthias Otten
2017-6-8
编辑:Matthias Otten
2017-6-8
the following did it for me. Setting STDOUT was not necessary
setenv('GFORTRAN_STDIN_UNIT', '5')
setenv('GFORTRAN_STDOUT_UNIT', '6')
setenv('GFORTRAN_STDERR_UNIT', '0')
!myfortran < Input.txt
setenv('GFORTRAN_STDIN_UNIT', '-1')
setenv('GFORTRAN_STDOUT_UNIT', '-1')
setenv('GFORTRAN_STDERR_UNIT', '-1')
The following threads contain the needed Information. Thank you Walter Roberson and Jan Simon. https://fr.mathworks.com/matlabcentral/answers/44388-or-system-or-unix-input-redirection https://fr.mathworks.com/matlabcentral/answers/91919-why-does-the-output-of-my-fortran-script-not-show-up-in-the-matlab-command-window-when-i-execute-it
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
