Not same fprintf MATLAB and Linux Console
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hello everybody,
I am trying to catch an error and I have made the following code in Matlab:
catch ME
% Some error occurred if you get here.
errorMessage1 = sprintf('Error in function %s()\n',ME.stack.name);
errorMessage2 = sprintf('Error in line %d\n',ME.stack(1).line);
errorMessage3 = sprintf('Error %s()\n',ME.message);
fprintf(2,'-----------------------------\n');
fprintf(2,'%s\n', errorMessage1);
fprintf(2,'-----------------------------\n');
fprintf(2,'%s\n', errorMessage2);
fprintf(2,'-----------------------------\n');
fprintf(2,'%s\n', errorMessage3);
The output of Matlab is the following one:
Error in hello()
Error in line 386
Error Index exceeds matrix dimensions.()
But, when I execute matlab from de Linux console:
./matlab < hello.m
I just get this output:
Index exceeds matrix dimensions
I cannot see the line or the .m I am running.
What kind of problem is this?
Thanks in advance,
Javi
채택된 답변
If you write the message to stderr by
fprintf(2, '%s\n', errorMessage1);
you have to catch this channel also. Try:
fprintf(1, '%s\n', errorMessage1);
Perhaps this works instead, but I cannot try it currently and hopefully someone corrects me:
matlab -r hello.m 2>&1
댓글 수: 12
Can you explain me 2>&1 what exactly does?
I used fprintf(1,..) and it did not work...
Jan
2017년 10월 10일
But 2>&1 works? I did not work with Linux for 20 years now...
This redirects the stream 2 (stderr) to 1 (stdout). See https://en.wikipedia.org/wiki/Redirection_(computing).
Do you see that I have used "matlab -r hello.m" instead of "matlab < hello.m"?
Thanks for your help.
But it seems not to work...
I just get the fprintf of the error, not the line or function
Jan
2017년 10월 11일
This is not clear: What exactly is "the fprintf of the error"? Do you get any output from stdout? I'm surprised that fprintf(1, ...) does not work.
Javier Naranjo
2017년 10월 12일
편집: Javier Naranjo
2017년 10월 12일
The problem is that when I execute the script in MATLAB and I get an error I can see the 3 fprintf in the console (MATLAB console) (errorMessage1, errorMessage2,...)
On the other hand, when I execute in the Linux Console ./matlab < hello.m and I get an error it does not show any of the errorMessage and I only get for example: Index exceeds matrix dimensions.
I do not see the fprintf of the line or the function.
Thanks for your help
Jan
2017년 10월 15일
Javier Naranjo
2017년 10월 17일
편집: Javier Naranjo
2017년 10월 17일
Thanks you so much! It worked! I am running hello.m from another script that opens and closes a diary.
The little problem now is that I get:
No MATLAB command specified
in the Linux Console when I run:
sudo ./matlab -r < getOutput.m -nosplash -nodesktop
It seems to be a warning... any idea how to solve this issue?
Thanks again!
Try
./matlab -r getOutput.m -nosplash -nodesktop
without the <
Javier Naranjo
2017년 10월 17일
편집: Javier Naranjo
2017년 10월 17일
If I do not write down "<" and I execute
./matlab -r folder1/getOutput.m -nosplash -nodesktop
It turns:
not variable or function folder1
and it stays in an infinite loop.
Try to use quotes and provide an absolute path:
./matlab -r "~/folder1/getOutput.m" -nosplash -nodesktop
Or single quotes ' ?
/folder1/getOutput.m
|
Error: Unexpected MATLAB operator.
I tried with single and doble quotes and it did not work.. When I write down double it says too many input arguments.
Thanks
@Javier: Sorry, my mistake.
./matlab -r "cd('~/folder1'); getOutput" -nosplash -nodesktop
The parameter of the -r command must contain a valid Matlab command, not the path of a m-function. You can find more details in the documentation: https://www.mathworks.com/help/matlab/matlab_env/startup-options.html
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
참고 항목
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)
