Error using get Invalid handle .
显示 更早的评论
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)

%
采纳的回答
更多回答(2 个)
Image Analyst
2015-8-7
1 个投票
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 个评论
majed
2015-8-7
Image Analyst
2015-8-10
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.
Walter Roberson
2015-8-7
1 个投票
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!