Error using get Invalid handle .

3 views (last 30 days)
majed
majed on 7 Aug 2015
Commented: Image Analyst on 10 Aug 2015
Hi ,
I am trying to load a file using matlab GUI. I need to have access to the file in 2 different .m file.
I have exactly the same code in both of them. the problem is that when I fill the edit boxes and want to run the second .m file, I can`t read the file name from the edit box that I stored the name there before.
My main GUI .m file for pressing a button (and working OK) :
% --- Executes on button press in show_input_noise.
function show_input_noise_Callback(hObject, eventdata, handles)
% hObject handle to show_input_noise (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.infobox,'String', handles.info.input_noise);
if (handles.input_noise_file_on == 1)
figure
file_name_input = get(handles.input_noise_file,'String')
path_root = get(handles.root_dir_text,'String')
Complete_noise_file_Name = fullfile(path_root, file_name_input)
if(handles.input_xls_on ==0)
s = load(Complete_noise_file_Name);
L_in = s(:,2);
f_in = s(:,1);
semilogx(s(:,1),10*log10(abs(s(:,2))))
else
s = xlsread(Complete_noise_file_Name);
L_in = 10.^ (s(:,2)/10);
f_in = s(:,1);
semilogx(s(:,1),s(:,2))
end
grid
axis([1e3, 1e7, -160,-60])
else
% handles.crystal_bw = 1e8;
pn_crystal_leeson(handles.input_noise,10^5,handles.vco_frequency/handles.N,5e3,-155,handles.crystal_bw);
title('Input Noise PSD');
xlabel('Frequency (Hz)');
ylabel('dBc');
end
guidata(hObject, handles);
in the next .m file when I call the function in following lines I get error of invalid handle. :
file_name_input = get(handles.input_noise_file,'String')
path_root = get(handles.root_dir_text,'String')
Complete_noise_file_Name = fullfile(path_root, file_name_input)
%

Accepted Answer

majed
majed on 7 Aug 2015
I found the problem.
I just needed to add following line at the end of CreateFcn function of the texfile object.
handles.root_dir_text = gcbo;
guidata(hObject, handles);
and the same thing for the other text boxes. so I can have access to them later again .
  2 Comments
Image Analyst
Image Analyst on 8 Aug 2015
I'm pretty certain that is not true. I use edit fields all the time and I have NEVER had to add any code to the CreateFcn in order to access any field of handles later, or any value of any control on the GUI.
You're using GUIDE, so why do you have two different m-files anyway?
majed
majed on 10 Aug 2015
That is strange for me as well. that is why I couldn`t understand the problem . I still can`t understand why recalling guidata() in creatfcn function solved the problem!!!! . but that was the solution for me. if I remove that, the problem will show up again!!.
I have different .m file to do different calculations. in each .m files i need to read the values in the text files. or change them based on the calculations for further data inputs.
I have different .m files to manage and debug the system easier. if I wanted to put them all in one .m file it would be more than 10k line and moving between codes was a pain .

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 7 Aug 2015
The next m-file apparently doesn't know about the prior m-file's handles variable because you didn't share it. If you don't do something to share it, then each file has it's own private set of variables. See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
  2 Comments
majed
majed on 7 Aug 2015
Thanks for response.
no it is not true. I only have problem with this get command. for example if you look at the code. I have other use of handles as well. they are completely ok. lik :
if (handles.input_noise_file_on == 1)
I can check the status of the check box
or before this line in second .m file I have :
r1 = handles.r1;
r3 = handles.r3;
c1 = handles.c1;
c2 = handles.c2;
c3 = handles.c3;
all of them are other edit boxes but integer values. they are all ok . I can`t share the whole .m file since I am not allowed to do that. sorry .
Image Analyst
Image Analyst on 10 Aug 2015
I get the feeling that when you say "second .m file" you really mean a second function in the same m-file. Of course if you have two separate GUIDE-built m-files (two separate GUI programs), then "handles" in each one is totally different from each other even though they have the same name.

Sign in to comment.


Walter Roberson
Walter Roberson on 7 Aug 2015
For the purposes of debugging, add a UserData to handles.input_noise_file, where the UserData is an onCleanup() routine that uses dbstack() to show where the cleanup has been called from. This should allow you to see where the handle is getting destroyed.
  1 Comment
majed
majed on 10 Aug 2015
That was very good comment Walter. I will definitely use this in future. Thanks for sharing your experiences. Majed

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps 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!