Error using get ......Invalid handle???!!!!!

Good after noon everyone
I'm trying to build a GUI that would match an input image and a database of images...I'm using the edge function
but when calling the database I get the following error
Error using get
Invalid handle
That's the function of the button that calls the database
function source_Callback(hObject, eventdata, handles)
load Letters
global Letters;
num=size(Letters,2);
q = zeros (1:num);
for n=1:num
handles.Y=Letters{1,n};
q(n) = get(handles.Y,'n');
guidata(hObject,handles);
end
handles.output = hObject;
Thats the comparing function
load Letters
global Letters;
num=size(Letters,2);
matched = 0;
k = handles.XX;
k = rgb2gray(k);
edge_k= edge(k,'prewitt',[]);
display('testing');
for n=1:num
p = handles.Y;
edge_p = edge(p,'prewitt',[]);
for i = 1:1:400
for j = 1:1:400
if(edge_p(i,j))==1&&(edge_k(i,j)==1)
matched = matched+1;
disp (matched);
end
end
end
and finally the function used in creating the database
clear all;
close all;
clc;
for i = 1:50
A = imread(strcat('Sample 2\A\a',int2str(i),'.png'));
A = rgb2gray(A);
letter=A ;
Letters(1,i)=mat2cell(letter,2048,[1536 ] );
end
save ('Letters','Letters')
Please help me correct the error
when I removed the get function when matching it displayed the same number of matched point for all 50 images
Thank you

Risposte (2)

Image Analyst
Image Analyst il 10 Mag 2015

0 voti

Never use "clear all;" inside a callback function. It will blow away your handles structure so that you can no longer access it.

12 Commenti

Hello Sir Image Analyst and thank you for your time Actually I'm creating the database in a separate function from the GUI it's in another script In mt GUI I'm only calling the database
It is pretty much the case that you should never be using "clear all" inside a script or function; that if you ever need it, use it only at the MATLAB command line.
I know some people disagree with me but I find it can be useful in the case where a beginner is working on a single script . All too often I've seen where the user will change a name of a variable, but not everywhere in the script, and when the script runs again it uses the old value and the user is totally confused and posts here that the variable is magically getting weird values. A clear prevents this common problem (which has even tripped me up on occasion).
Of course if you're working on multiple scripts at the same time (which I personally would not recommend especially for beginners), then a clear in one script would blow away variables in the other script. I don't think beginners have two scripts going at a time where they need to keep variables from script1 in the base workspace while they start running script2, so this will not be a common drawback.
Of course if the user transfers that script into a callback function then they will need to remove the clear. And actually we see posts asking about this situation also (where they forgot to remove the clear).
Instead of teaching them "clear all", teach them to add
function MyFileName
at the beginning, where MyFileName refers to the name of the .m file. No more problem with hanging values ;-)
Well yeah, but then you have the people complaining that their variables are no longer visible in the workspace after they run their script. Of course they could set a breakpoint on the last line. I guess it's just a matter of style and how you like to operate.
Jan
Jan il 10 Mag 2015
Modificato: Jan il 10 Mag 2015
I'd dare to agree, that clear all is useful, when a beginner works with a single script, which does not contain bugs - but I've seen too many questions in Matlab forums, which could be solved easily using the debugger.
thanks all for your help....I removed clear all; but it got nothing to do with the errors I'm receiving
but I must say that as a beginner myself I found it much easier to work with a single script than in many separate functions that's why I'm using GUI functions
Please attach your latest script with the paper clip icon.
when I type
grayImage = Letters{1, 35};
imshow(grayImage, []);
it gives me an error
letters should contain 100 images 50 for A (a1 --> a50) and 50 B (b1 --> b50)
instead it contains only 2 images a50 and b50
I meant attach the whole m-file, not just 2 lines. Also, you didn't say what the error was. That looks like it should have worked.
and i need values to be in this format (that is the gray scale matrix of each image)
I'm about to give up here. You keep changing your code and don't even attach it like I requested. First you say:
Letters(1,i)=mat2cell(
then you say
Values=mat2cell(
And did you know letters and Letters are different because MATLAB is case sensitive ? Anyway, these are the two links that you need to solve all your MATLAB problems:
and
Good luck.

Accedi per commentare.

Your call
Letters(1,i)=mat2cell(letter,2048,[1536 ] );
is going to be the same as
Letters{i} = letter;
The mat2cell() only has the effect of breaking up the matrix into smaller pieces if the following arguments include at least one non-scalar vector. When both of the values are scalar, the effect would be to give an error message if the array was not 2048 x 1536, and then to wrap the matrix inside a cell array.
Notice that what you store into Letters{i} is not an object, just a numeric matrix. As it is not an object at all, it has no property 'n' to be fetched by get().
I suspect that what you are looking for is something like,
Letters(1,i) = struct('letter', letter, 'n', n );
and then in your callback,
handles.Y = Letters(n).letter;
q(n) = letters(n).n;

11 Commenti

Nour Mawla
Nour Mawla il 10 Mag 2015
Modificato: Walter Roberson il 10 Mag 2015
Hello Sir Walter Roberson...thank you so much for your time and effort.
I did the adjustments you asked of me and there is some questions I need to ask you
sir the database is created in a separate function and thus the variable 'n' is not defined there. How am I supposed to define it in the function
I tried Letters(1,i) = struct('letter', letter, 'i', i );
but when I tried
handles.Y = Letters(n).letter;
q(n) = letters(n).n;
the callback function displayed this error
Undefined variable "letters" or class "letters".
Error in push>source_Callback (line 59)
q(n) = letters(n).n;
sir really thank you very much for your help and forgive me but I'm new to matlab and I tried everything I know...I'm desperate :'(
q(n) = Letters(n).n;
But first you need to describe what you wanted
get(handles.Y,'n');
to mean. What value are you trying to recall from the original data?
Nour Mawla
Nour Mawla il 11 Mag 2015
Modificato: Walter Roberson il 11 Mag 2015
hello Sir and sorry for taking so long to response
what I'm trying to do is set the images in the callback function so that it would automatically call all images in database and then compare them with the sampled image
and then in the match callback function the sampled image would be compared with each image in the database and return the closest one
by get(handles.Y,'n'); I'm trying to call each image in the database separately and start the matching using the edge function
however I discovered a new error
while creating the database in this function
for i = 1:50
A = imread(strcat('Sample 2\A\a',int2str(i),'.png'));
A = rgb2gray(A);
letter=A ;
Letters(1,i)=mat2cell(letter,2048,[1536 ] );
end
only image 'a50.png' is saved in the database the others won't be saved
I tried everything I know but just didn't work.....:'(
There's something you're not telling/showing us. As long as "i" is different, then you're assigning a cell to the first row, i'th column of Letters. Why do you say that it's not saving? If you do this:
grayImage = Letters{1, 35};
imshow(grayImage, []);
are you saying that it does not display image #35? It displays image #50 instead?
Nour Mawla
Nour Mawla il 11 Mag 2015
Modificato: Walter Roberson il 11 Mag 2015
Sir Image Analyst.....i guess that the naming is causing some confusion let me rename them to tell you where I'm facing the error
for i = 1:50
A = imread(strcat('Sample 2\A\a',int2str(i),'.png'));
B = imread(strcat('Sample 2\B\b',int2str(i),'.png'));
A = rgb2gray(A);
B = rgb2gray(B);
letter = [A B] ;
Value = mat2cell(letter,2048,[1536 1536 ] );
end
save ('Database','Value')
the error that is going on is that letter [A B] is only saving the values of 'a50.png' and 'b50.png'
I can't change the declaration of any of the variables I need their type to remain the same or the matching won't work
what should I do?
Nour Mawla
Nour Mawla il 11 Mag 2015
Modificato: Walter Roberson il 11 Mag 2015
I fixed the error in the callback function it worked when I did this:
handles.Y =Values{1,n};
q = handles.Y;
However the problem is in the database since it doesn't contain all the values....how can I fix it :'(
Value{i} = mat2cell(letter,2048,[1536 1536 ] );
It is only saving a50 and b50 because you are overwriting letter and the Value that you calculate from it every time in a loop so by the end of the loop you only have the last ones left.
You need to store all your values in an array e.g.
Value{i} = mat2cell(letter,2048,[1536 1536 ] );
then you will have them all still after your loop ends.
Sir Adam and Sir Walter....thank you so much for you time
I already tried that but it will give me an error
look at the difference in the saved value page
for i=1:2
Value{i} = mat2cell(letter,2048,[1536 1536 ] );
but I need it to be
otherwise an error would appear in the matching function.
this function does the job
Value = mat2cell(letter,2048,[1536 1536 ] );
I can't change the matching function because it does 100% what I need it to do and honestly fixing the data base is easier (took me 3 weeks to finish the matching function...I made it from scratch)
Value(i) = mat2cell(letter,2048,[1536 1536]);
Adam
Adam il 11 Mag 2015
Yeah, I couldn't remember which it needed to be and didn't have time to remember how to use mat2cell to test it so I went for the wrong one!

Accedi per commentare.

Categorie

Scopri di più su Interactive Control and Callbacks in Centro assistenza e File Exchange

Richiesto:

il 10 Mag 2015

Commentato:

il 11 Mag 2015

Community Treasure Hunt

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

Start Hunting!

Translated by