Printing a message inside a function
조회 수: 56 (최근 30일)
이전 댓글 표시
Hi everyone,
I'm new to using and creating my own functions, however, I'm having some trouble getting a simple message to print inside my function.
I want the function to run a script (file_name), which it does and I am happy with that. But I want that message (file1 'has finished running') to be displayed so that when I change this function to run say 3 other scripts, I will know when each have finished running.
So here I define the function:
function run_other_code()
global file1
file1 = fullfile(['path'],'file_name.m');
run(file1)
print([file1 'has finished running'])
end
And here is where I call it, in another script:
run_other_code()
It runs up to the print line, absolutely fine, and does the job of running the other script. But then it fails and I get the following error message:
Reference to a cleared variable file1.
Error in run_other_code (line 5)
print([file1 'has finished running'])
I just can't work out why it doesn't work, I made file1 a global variable as I thought perhaps it was getting lost after it has been used first.
But I'm just not sure how to fix it. If anyone could help that would be brilliant - thank you.
댓글 수: 0
채택된 답변
Star Strider
2022년 5월 13일
Use either:
sprintf('%s has finished running',file1)
or:
fprintf('\n%s has finished running\n',file1)
depending on the result you want.
댓글 수: 2
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!