MATLAB: Invisible figure handles behave differently on Windows and Linux? - matlab

I always thought that, in MATLAB, a graphic handle X whose property HandleVisibility is set to anything else than on will not show up when using findobj(h) or get(h, 'Children') with h being the parent of X. However, this seems to be true only under Windows and not under Linux. I am using MATLAB R2011b both on Debian 6.0.6 (squeeze) and Windows 7. If I run the following code under Windows:
figure;plot(randn(1,1000));
h = get(gcf, 'Children');
Then I get a single handle in h, which corresponds to the axes that contain my random plot. This is what I would expect. However, if I run exactly the same code in Linux, h contains an array of 10 handles. Indeed most of those handles are just UI elements, whose HandleVisibility property is set to off. For instance:
get(h(end), 'Type') % returns: 'uitoolbar'
get(h(end), 'HandleVisibility') % returns 'off'
Is there a reason for this apparently inconsistent behavior? Can this be reproduced by others? In case it could be relevant, the Debian server that I am using runs Sun JAVA 1.6.0_26, which is not the default on Debian (openJDK).

I was unable to reproduce on either r2011a or r2012b, with Sun JAVA.
One workaround might be to filter based on visibility:
visibleChildren = findobj(get(h,'children'),'HandleVisibility','on')
Sounds like something specific to your install.

Related

How can I write with latex character in xlabel in matlab

I would write my xlabel with Latec character so I used this code
x = -10:0.1:10;
y = [sin(x); cos(x)];
plot(x,y)
xlabel('$\mathbb{x}$','Interpreter','latex')
but I have this warning message
Warning: Error updating Text.
String scalar or character vector must have valid interpreter syntax:
$\mathbb{x}$
and the xlabel appear like this
https://i.stack.imgur.com/NCI4n.png
please how I can fix this problem.
The problem with your example is the mathbb command, which is not in base Latex. To show this, replace the \mathbb with, e.g., \mathrm. This will work.
I've never seen an example of adding Latex packages into the Matlab environment. (Update below)
You can have a look at this question, which includes a very aggressive potential way to add the package you need. But it's not clear to me if the solution proposed is actually functional.
How do you use the LaTeX blackboard font in MATLAB?

Default "legend" function overwritten by accident

I am using the BNT-toolbox, a big library written in matlab for inference in bayesian networks.
I had to add this toolbox to the path of MATLAB. But after doing that I can't use the default legend function any more.
I think that this library might have his own legend function, overwriting the default one. How can I manually tell MATLAB that I want the original one and not the one in the new toolbox?
Tried in Matlab 2018b and 2020a
EDIT: to reproduce it:
When I run the testscript, it shows the lines and the legend.
https://github.com/bayesnet/bnt, this is the toolbox I talked about. I downloaded it, unzipped and then added it to my path with Home -> Set path -> add folder with subfolder
When I run the script now, it shows the lines and not the legend.
NOTE: when I tried another way of plotting (see testscript 2), the legend shows itself again. So this is a working "workaround"
Testscript1: (location: C:\Users\TomDe\Downloads\FullBNT-1.0.7\bnt\own\testscript1.m)
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)
hold on
y2 = cos(2*x);
plot(x,y2)
legend('cos(x)','cos(2x)')
Testscript2
% Some other code
tiledlayout(2,1)
nexttile
plot(inputPath)
hold on
plot(sensorPath)
plot(inputInference)
hold off
title('The Input sequence and sensor readings ')
legend('Path', 'sensor', 'Inference')
You can check that that is indeed the case with the which function:
>> which legend -all
It's generally a bad idea to overshadow MATLAB's own functions. I highly suggest you avoid this problem in the first place. Create a MATLAB package and place the source code of this toolbox in there.
For demonstration purposes only, I'll show how to call the real legend.m:
>> wd = pwd;
>> cd 'C:\Program Files\MATLAB\R2020a\toolbox\matlab\scribe\'
>> legend(...)
>> cd(wd);
this being the location of the file on a MATLAB R2020a install.
There are two things you can do:
You always want to use the default legend, never the one in the toolbox: use the -end option to your addpath call when adding the BNT toolbox directory, so that its functions appear at the end of the path. MATLAB will always find functions by looking through the path directories in turn, the directories earlier in the path therefore have precedence.
You want to use both versions of legend, and want to choose which one to use: write a little support function that removes the BTN toolbox from your path, calls legend, then adds the toolbox back in. Such a function looks like this (save it as original_legend.m somewhere in your path, then use it in the same way you'd call legend but using this new name instead):
function out = original_legend(varargin)
rmpath /path/to/bnt/toolbox
out = legend(varargin{:});
addpath /path/to/bnt/toolbox

hit.IntersectionPoint returns NaNs in matlab's ButtonDownFcn

I have developed a GUI with Matlab's AppDesigner. To pick up mouse clicks, I set the ButtonDownFcn callback for the image I have plotted. Then inside the callback I read the hit.IntersectionPoint:
ax = app.UIAxes;
ih = imagesc(I, 'Parent', ax);
ih.ButtonDownFcn = {#im_ButtonDownFcn, app};
_
function im_ButtonDownFcn(im, hit, app)
mouse_pos = flip(round(hit.IntersectionPoint(1:2))); %[NaN NaN NaN]
On my computer everything works, but on a colleague's computer, it returns NaNs. As far as we can tell, the differences are:
Mine (works) Colleague (NaN)
Win10 MacOS
USB Mouse Laptop Trackpad
Does anyone know if either of these could be a factor in IntersectionPoint returning NaNs? Or have another suggestion that we could troubleshoot?
I have read here that the IntersectionPoint is only available from 2018a onwards. My colleague uses 2017b, but when I tested on 2017b on my computer, the IntersectionPoint still worked.

face detection for matlab

i searched face detection for matlab to my project.
i found one:
http://people.kyb.tuebingen.mpg.de/kienzle/fdlib/fdlib.htm
i downloaded the source code, but it didn't work, i got that error from matlab:
??? Undefined function or method 'fdmex' for input arguments of type
'uint8'.
Error in ==> tinytest at 10 s = fdmex(x', threshold);
the main script is:
x = imread('geeks.jpg');
% decision threshold.
% change this to a smaller value, if too many false detections occur.
% change it to a larger value, if faces are not recognized.
% a reasonable range is -10 ... 10.
threshold = 0;
imagesc(x); hold on; colormap gray;
s = fdmex(x', threshold);
for i=1:size(s,1)
h = rectangle('Position',[s(i,1)-s(i,3)/2,s(i,2)-s(i,3)/2,s(i,3),s(i,3)], ...
'EdgeColor', [1,0,0], 'linewidth', 2);
end
axis equal;
axis off
can you find the error?
Usually when I see a uint8 error and a grayscale image, its a red flag to me that I need to do
colorImg=imread('imageName.jpg')
% Even if the image is grayscale, if its png or jpg,
% it will load in as a color image almost exclusively
img=rgb2gray(colorImg)
If you look at the img output, you will notice now its of type double instead of uint8 :)
If that doesn't work, hopefully macduffs will, mine just seems easier if that actually does fix it. :)
Depending on your version of matlab, it looks like the fdlib, comes with a .dll, rename it to .mexw32 or whatever your host machine desires. You can determine this by running:
>> mexext
mexw32
on the Matlab command prompt. Use the mex extension and rename the fdmex.dll to fdmex.mexw32, or whatever mexext returns and it should run flawlessly.
If I run in on my Windows XP machine, I get that beautiful picure:
However, if you do not have a 32 bit machine, the author of the software writes on the link in the question:
Please note that all builds were optimized for Intel Pentium CPUs. If
you would like to run it on a different platform, or have any other
questions, please let me know.
He has a link to his profile and email, so I recommend contacting him for a 64 bit version of the executable.
If you have a recent version of Matlab with the Computer Vision System Toolbox installed, you can use vision.CascadeObjectDetector system object to detect faces in images.

Setting the value of an array control of a Labview VI through ActiveX (with Matlab)

I have a Labview VI that I intend to run from Matlab through ActiveX. It has one argument (that is, one Labview control), which is of type 1D numeric array.
The method used to call the VI from Matlab through ActiveX is detailed in a previous post.
I am trying to set the value of this array control in Matlab before running the VI (that is, Matlab will pass an argument to the VI and then run it; no action is to be performed manually through the Labview interface).
Getting the value through the GetControlValue method works fine (I get a nice Matlab array). However, when I try to set the value of this same control with SetControlValue using the value returned by GetControlValue, the value of the control becomes empty (as evident from the value Empty matrix: 1-by-0 obtained by Matlab after using GetControlValue again, and in Labview where the values of the control become grayed-out).
The same procedure works perfectly when the control is a single numeric value.
What is going wrong here exactly ?
See the screen capture below:
You can compile the VI to a DLL and call your function that way. This abstracts away LabVIEW's typesystem and its COM runtime.
Can you provide more detail about the problem you are trying to solve?
(Source: 0utlaw on the NI forum).
A useful workaround to this problem is to use a Matrix control in Labview.
Matlab can then pass usual arrays, and Labview maps these Matlab arrays to the matrix. Works as expected with 2D arrays as well.