Matlab editor: the function might be unused

95 ビュー (過去 30 日間)
Deerlin
Deerlin 2014 年 10 月 1 日
コメント済み: Mustafa Akgül 2020 年 12 月 9 日
Below is the body of my code.It keeps saying that func2 might be unused.I'm not sure how to fix this...
  3 件のコメント
Walter Roberson
Walter Roberson 2017 年 5 月 16 日
Chao Yin: that is not correct. You can define any number of functions in the same .m file, without them needing to refer to each other. You might not be able to execute any function other than the very first unless you make special arrangements, but it is valid to define them.
Mustafa Akgül
Mustafa Akgül 2020 年 12 月 9 日
How can I define

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

採用された回答

Julia
Julia 2014 年 10 月 1 日
編集済み: Julia 2014 年 10 月 1 日
Hi,
the easiest way is to make two files: one for func1 and one for func2. (I don't know if you can fix this problem using only one file.) In the file for func2 you have to add the Global-definition for G.
The file names must be the same as the function names.
  3 件のコメント
Guillaume
Guillaume 2014 年 10 月 1 日
It's clear to me from the wording of the question that func1 and func2 are in two different files.
There's no way in matlab to call func2 from the matlab prompt, if it's in the same file as func1. If func2 is defined within func1 file, only func1 would be able to call fun2 and as it's not the case, matlab rightly warns you it's not used.
Julia
Julia 2014 年 10 月 1 日
編集済み: Julia 2014 年 10 月 1 日
So you don't have to implement those functions, you have to understand how global variables work.
It is really not complicated:
func1 can manipulate the global variable G since G is defined as global in this function. Whereas in func2 G is only a local variable.
Post your guesses for a, b, and G here and we will be able to tell you if you understood the concept right.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 10 月 1 日
func2 was never used, never called, in func1. You just assigned y to some equation, which happened to be similar (but not the same) equation as in func2. You never said
y=fun2(x)
anywhere in your main function, func1.
You also need the line
global G;
inside func2 if func2 is to use the same value of G that func1 had. If you do that, and have the G=50 line, then it will also become 50 once you get back inside func1.
  2 件のコメント
Julia
Julia 2014 年 10 月 2 日
I think the task of this homework is to understand how the given functions work. And that it is intended that func2 does not use the global variable G.
Image Analyst
Image Analyst 2014 年 10 月 2 日
編集済み: Image Analyst 2014 年 10 月 2 日
If it's not global, then she must pass it in via the input argument list. That's perhaps the best way. Other ways of getting the same G inside func2() (if you need the same G in there) are discussed in the FAQ http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F. Of course maybe they don't want that and want you to see how the G in func2() is not the same value as the one in func1().
The homework problem instructions pass in G as part of an expression which is evaluated so it's not really passed in. Even if it were, it's overwritten with a value of 50, but since it's not global and not passed out, the 50 value won't be seen outside of func2().

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by