現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
if loop inside matlab function ----- i dont understand why this is not working
11 件のコメント
The above rewrite works but I'd suggest
function y =fcn(u) y = u>0.7 & u<0.71; end
NB: If the argument u is not a scalar then the result will probably never be true; the result of an logical operation on a vector or array is also a vector or array of the same size and if is true IFF all elements of the vector are true
The ML editor probably flagged (with the orange line in RH margin) about the use of && for scalar as well...
回答 (1 件)
Here is a event based solution to latch the value. The solution use if block and if action subsystem to latch the output once a certain condition is met. I used the clock as the input and the if condition becomes true between 20 to 21 seconds but output remains 1 after 20 seconds.
![](/matlabcentral/answers/uploaded_files/114252/latching.png)
The Simulink model file is also attached.
13 件のコメント
I wondered when I saw "latch" in the second comment if that might not have been OP's purpose.
"... is not possible with MATLAB function block."
While I'd never recommend for the purpose with Simulink, it is possible with Matlab by using persistent to retain state between function calls
Hi dpb,
i want to be sure that i understood correct. The following code would work for this purpose and also would latch the value for y between the several functions calls, but is not recommended for use in Simulink?
function y = myfun(u) persistent value if(u>0.7) && (u<0.71) value=1; end y = value; end
Is there any reason why it should not be used this way?
best regards
Stephan
That won't latch if that's the purpose, no...as coded it will still return the value based only on the input value of u; the persistent value isn't used to determine whether or not to reset its value.
CLARIFICATION It will latch if the condition on u is satisfied, that is true; it will fail on first call for lack of value being assigned; that it has been declared persistent will keep from generating an undefined error but the function will return empty which likely will cause an error in the calling routine expecting a value.
The function would also have to be called to initialize the persistent variable first before use.
If one had need for such a function in Matlab base product that would be the way to implement it; in Simulink there's better ways given its manner of operation.
I was simply pointing out that there is (and doc says before R2006 altho I didn't think it had been that long) the facility in ML.
One (unoptimized) implementation...
function y = myfun(u,init) persistent value if nargin==2 value=0; y=value; return end if value==1 y=value; return end if u>0.7 & u<0.71 value=1; y=value; end end
参考
カテゴリ
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)