How to define and call a function in the same script

74 ビュー (過去 30 日間)
Zied
Zied 2016 年 11 月 18 日
コメント済み: Walter Roberson 2016 年 11 月 18 日
Hello, I have a code that is working. It consists of a main script that calls 2 functions from 2 other m files. I will simulate my code in another program (Labview) and I need to put all the code in one script. Is it possible to define the function and call them in the same file. Thanks, Zied

回答 (3 件)

James Tursa
James Tursa 2016 年 11 月 18 日
Does it have to be a script? Can you simply wrap your script in a dummy function name? Then you could simply attach your other functions to the end of this file and have everything in one file. E.g.,
function scriptfun % <-- dummy name
% put your original script here
end
% then append your functions here

Walter Roberson
Walter Roberson 2016 年 11 月 18 日
It is possible as of R2016b, but not in any previous version. For any previous version you either need to use separate files or you need to convert the script into a function the way James shows.

Zied
Zied 2016 年 11 月 18 日
So, do you mean I need just to write my function just after the main code in the same file. Do you think it will work if the second function is nested in the first function?
Thanks
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 11 月 18 日
If you are using R2016b or later, then nested functions will work. For example file testscript.m
disp('this is a test script')
result = test_outer_function()
function r = test_outer_function
r = test_inner_function;
function s = test_inner_function
s = 11;
end
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLabVIEW についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by