hit.IntersectionPoint returns NaNs in matlab's ButtonDownFcn - matlab-app-designer

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.

Related

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

Showing the figure window with octave

I am a very newbie to octave.
I want to show the figure I plot with octave, but no figure window shows up.
Here is what I want to plot, plot.m
x = load('ex2x.dat');
y = load('ex2y.dat');
figure % open a new figure window
plot(x, y, 'o');
ylabel('Height in meters')
xlabel('Age in years')
I run the script with the command line directly
octave plot.m
The dataset can be downloaded from http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearning&doc=exercises/ex2/ex2.html
I have installed image package as the site suggested.
My OS is ubuntu 14.04, I am not sure if I miss something.
I appreciate any help.
Thank you!
I found the answer which can be found in Octave's FAQ.
http://wiki.octave.org/FAQ
If you are running an Octave script that includes a plotting command,
the script and Octave may terminate immediately. So the plot window
does show up, but immediately closes when Octave finishes execution.
Alternatively, if using fltk, the plot window needs a readline loop to
show up (the time when Octave is sitting around doing nothing waiting for interactive input).
A common solution is to put a pause command at the end of your script.
So I just to add pause in the end of my script and it shows the window.

Use Matlab PDE toolbox from command line

I would like to solve a PDE with Matlab PDE toolbox using only the command window of the system. I can create the problem and run the solver, but the PDE toolbox window pops up occaisionally and asks questions (e.g., "Do you want to save unsaved document?").
How can I avoid these popups or how can I use the PDE toolbox without opening its window?
I am using the following code. The window is pops up when I call the pdeinit function on the first line.
[pde_fig,ax]=pdeinit;
set(ax,'XLim',[-0.1 0.2]);
set(ax,'YLim',[-0.1 0.2]);
set(ax,'XTickMode','auto');
set(ax,'YTickMode','auto');
% Geometry description:
pderect([0 0.05 0.05 0],'R1');
pderect([0 0.1 0 0.1],'R2');
set(findobj(get(pde_fig,'Children'),'Tag','PDEEval'),'String','R2-R1');
...
The help for pdeinit is short: "Start PDETOOL from scripts." pdetool, like most *tool M-files from The MathWorks, is a GUI and the help/documentaion for it indicates as much.
I'm confused because, not only does pdeinit open a figure window, but you're using it to return handles to the figure and axis of that figure. Your code then proceeds to manipulate those handles. You can't get those handles without first creating and opening a figure. Is the issue that you just want a regular figure window instead? If so, then you can replace [pde_fig,ax]=pdeinit; with:
pde_fig = figure;
ax = gca;
You can look at the code for pdeinit: type edit pdeinit in your command window. You'll see that all it does is open pdetool (unless it's already open) and return handles to the resultant figure and axis.
Additionally, pderect will open pdetool on its own. You're using a bunch of functions that are all tied to the PDE app. Many of the tutorials and examples on The MathWorks's site use this. You might check out this article on how to solve PDEs programmatically. The examples might also be helpful.

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.

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

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.