Know if Serialport is connected (in App Designer program); ishandle() always true
Show older comments
I open a serial port to my Arduino and it usually works. I'm trying to take care of various errors, for one of which I need to know if the port handle is there and connected or not. In Command window I can tell when ishandle(MyCom) returns a logical answer
ishandle(app.PicoCom)
ans =
0×0 empty logical array
But programatically determining if it's there, say with
ishandle(app.PicoCom) == false
ans =
0×0 empty logical array
Doesn't work, it's always true. How do I ask a false logical array if it's false and why does this have to be so hard? Does anybody really need an array response to ishandle(app.PicoCom)? if ~ishandle(app.PicoCom) should be enough.
I also tried if ~exist(...) and even isstr(app.PicoCom.Port) but they don't work when it's not a handle.
BTW, ishandle() is only described as for graphic objects and figures and the existing answer doesn't answer my question
4 Comments
Gavin
on 30 Jul 2024
Does anybody really need an array response to ishandle(app.PicoCom)?
For that particular function, if you passed an array into it I believe the answer will always be the same for all the elements (unless maybe you have a heterogenous array hierarchy where some of the classes are handle classes while others are merely HandleCompatible, but I'm not 100% certain off the top of my head if that's allowed.)
Some of the is* functions in MATLAB return a scalar true or false value regardless of the size of the input(s). islogical and isvector are two such functions. Generally these answer a question about the "structure" of the input: classes, sizes, complexity, etc.
islogical(true)
islogical(false(0, 0))
islogical(magic(4))
isvector(magic(4))
isvector(zeros(1, 0))
Others return an array whose size is based on the size of the input(s). isfinite and isfield are examples of these, as is ishandle. Generally these are answering questions about the contents of the inputs.
isfinite([1 2 3 NaN 5 Inf 7 8])
s = struct('foo', 1, 'bar', 2);
isfield(s, ["foo", "notAField"])
f = [figure, figure; figure, figure];
ishandle(f)
Calling isempty (a "structural" question) before calling ishandle is a reasonable approach. The all function may be of use as well.
g = gobjects(0); % makes a 0-by-0 array suitable for holding graphics handles
ishandle(g)
all(ishandle(g))
Gavin
on 31 Jul 2024
Gavin
on 1 Aug 2024
Accepted Answer
More Answers (0)
Categories
Find more on Serial and USB Communication in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!