do nothing command in matlab

315 ビュー (過去 30 日間)
joe khese
joe khese 2014 年 2 月 22 日
コメント済み: Ron Fredericks 2023 年 3 月 23 日
is there a do nothing command in matlab like the ';' in c and c++. i am trying to write a command that does nothing for a particular condition for example
if (condition)= true; do something; else do nothing; ( in c this would just be ';') end
Pls how can I do this in MATLAB??
  2 件のコメント
per isakson
per isakson 2014 年 4 月 29 日
編集済み: per isakson 2014 年 5 月 8 日
My "do nothing" function/method reads
function appropriate_name( varargin )
end
Patrik Ek
Patrik Ek 2014 年 5 月 7 日
That is fair enough, but as seen in the answer there is a much simpler way not requiring a function call. Still if you want to use it, I guess the little overhead it creates disappears in the rest.

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

回答 (4 件)

Ernst Kloppenburg
Ernst Kloppenburg 2017 年 7 月 20 日
matlab will accept an empty if body or else body, at least as of R2015b:
if condition
do something
else
% do nothing
end
  1 件のコメント
Ron Fredericks
Ron Fredericks 2023 年 3 月 23 日
The problem with using a "comment for do nothing" is that you can't set a break point on it during debug session.

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


the cyclist
the cyclist 2014 年 2 月 22 日
This code will work:
if false
disp('false')
else
;
end
But why would this ever be better than simply having no line at all?
if false
disp('false')
end
Just curious about your use case.
  4 件のコメント
Patrik Ek
Patrik Ek 2014 年 4 月 29 日
Well I guess it is a matter about taste. As the initial if was inside a switch statement, this may have motivated me to not use too many levels of if and switch. Also by adding an extra if there, it seems like a catch for not getting an error. This may not always be the case since not setting 'myvar' is as valid as the others. However, the calculation time is nothing to talk about (not even sure if there will ever be a difference) and the extra if is even the same number of lines, so I guess that I was overly happy about the do nothing statement.
Ron Fredericks
Ron Fredericks 2022 年 1 月 30 日
We can't set a breakpoint on a comment. So having a line of code like "noop" can be useful for debugging.

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


Yuxiao Zhai
Yuxiao Zhai 2022 年 10 月 26 日
You could create an anonymous function that takes arbitrary input arguments and outputs an empty array. Then executing this anonymous function is simply equivalent to doing nothing.
h = @(varargin)[];
% At the line where you want to do nothing=============
h();
% =====================================================
You could also feed h with any types of input arguments and it would still do nothing, like
h(1,2,3);
h('Hello world');
h(struct, table, cell(1), datetime);
Good luck

Ron Fredericks
Ron Fredericks 2022 年 1 月 30 日
編集済み: Ron Fredericks 2022 年 1 月 30 日
I prefer this line of code as equivalent to "noop" or "do nothing"
disp('') % Does nothing but allows developer to set a breakpoint here.

カテゴリ

Help Center および File ExchangeBloomberg Desktop C++ Interface についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by