do nothing command in matlab
이전 댓글 표시
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
2014년 4월 29일
편집: per isakson
2014년 5월 8일
My "do nothing" function/method reads
function appropriate_name( varargin )
end
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
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
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
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
Fyi, I have a case, where you will save yourself some annoying code by do nothing.
if exist('myvar','var')
;
elseif myFirstStatement
myvar = firstThing;
elseif mySecondStatment
myvar = secondThing;
...
else
myvar = defaultValue;
end
This code will set a value to a few default values if the value is not already set, but the default value may be dependent on some other parameters; Which means that you need to set another default parameter if no of the custom default parameters apply. It is of course possible to achieve the same thing with an extra else statement (or switch), which would then not require an if exist('myvar','var') in every elseif, by why not have a "do nothing" instead. It is no doubt more effective since you only need 1 if statement, it gives readable code since the " elseif s" are not really a sub-statement to ~exist('myvar','var') and third, my if statement is already inside a switch statement. I do not need a level more.
So to sum up, I do not thing that a "do nothing" is essential, but there is certainly nice to have sometimes, and then you are really glad that you have it. It would of course be possible to do myvar = myvar instead, but how does that look, really? However +1 for a useful answer.
the cyclist
2014년 4월 28일
Well. Hm. Maybe. But. Um. Hm.
I think the following slight modification of your code -- adding a not() to the first if statement -- is more intuitive, removes the need for the "do nothing" statement, and also addresses your (correct) observation that your first elseif was not really logically related to your initial if.
if not(exist('myvar','var'))
if myFirstStatement
myvar = firstThing;
elseif mySecondStatment
myvar = secondThing;
...
else
myvar = defaultValue;
end
end
What do you think?
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
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
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
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.
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!