User-defined function is "unrecognized" in command window.

This print_list function works in the script, but it gives the error message "Unrecognized function or variable 'print_list'" in the command window. I am having the same problem with all my other user-defined functions. How can I fix it?
global list list_idx;
list = ["1st", "2nd", "0"];
list_idx = [1, 1, 0];
print_list;
function var = print_list()
global list list_idx;
L = logical(list_idx);
var = list(L);
disp(var);
end

 채택된 답변

galaxy
galaxy 2019년 11월 5일

1 개 추천

You copy only print_list function to other file and save print_list.m in Matlab
It will be OK

댓글 수: 3

galaxy is correct. As the documentation states, "Local functions are only visible within the file where they are defined, both to the script code and other local functions within the file. They are not visible to functions in other files, and cannot be called from the command line."
If you want a function to be callable from the MATLAB Command Prompt or from other files, it needs to be the main function in its function file.
Thanks! Also, I figured out that the while calling the function, the name of the file containing the function should be used. In my case, I got errors with print_list() but it worked with function_print_list() since the file name for the function was function_print_list.m.
TY

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

질문:

2019년 11월 5일

댓글:

Ali
2023년 10월 15일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by