Getting value of selected string in Popup menu.
3 views (last 30 days)
Show older comments
Sidra Aleem
on 1 May 2017
Edited: Walter Roberson
on 4 May 2017
I am trying to make a GUI. Depending on the option selected in popup menu 3 the selected operation should be performed on images. However when i am trying to get the value from popupmenu3 I am getting the following error.
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)pr('popupmenu3_Callback',hObject,eventdata,guidata(hObject)).
Below is the code for this popupmenu.
function popupmenu3_Callback(hObject, eventdata, handles)
contents = get(hObject,'String');
a = contents{get(hObject,'Value')} ;
% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
When I make a simple GUI with only popupmenus I don't get the error. But when I try to get the value of selected string I am getting error.
4 Comments
per isakson
on 2 May 2017
Edited: per isakson
on 2 May 2017
- You should always show the entire error message.
- See Debug a MATLAB Program
- This folder name looks strange to me
127 cd(handles.Dir.Functions_Dir)
K>> handles.Dir.Functions_Dir
ans =
D:\ÎÒµÄÎĵµ\MATLAB\Image Processing\Functions\
K>>
Accepted Answer
Walter Roberson
on 2 May 2017
Your code does not protect against the possibility that there are no subdirectories in the selected directory. There are no prompts as to what the purpose of the selecting a directory is, so you should assume that users might select something with no subdirectories.
Your code should use fullfile() instead of splicing strings together in the line
handles.People(i).Images = ReadFileNames([handles.Dir.Home_Dir '\Inputs\' dirinfo(i,1).name]);
In Browse_Callback you do
foldername = uigetdir('D:\ÎÒµÄÎĵµ\MATLAB\Image Processing','Select folfer');
handles.fname=foldername;
which is fine in itself, and in pushbutton2_Callback you use handles.fname to refer to the selected directory and you find the sub-directories of that. But then just a few lines further down you use
cd(handles.Dir.Functions_Dir)
which assumes that handles.Dir.Functions_Dir is the same as handles.fname . Why bother to have two different variables for the same purpose? Why bother to ask the user what directory to use if you are always going to assume that handles.Dir.Functions_Dir refers to the same place? Or flip that around: if you bothered to ask the user what folder to use (perhaps because they are using a different machine where the folders are in a different location) then why are you are ignoring the user-selected folder name?
You do not need to do the cd() at all. You can use
handles.People(i).Images = ReadFileNames( fullfile(Input_Dir, dirinfo(i,1).name) );
By the way, in ReadFilenames, you should be using fullfile() instead of worrying about NameSeperator . Also, although it is not well known, internally MS Windows uses '/' as its directory separator, using '\' for public presentation. Because the real directory separator is '/' you can just always use '/' if you are going to code directory separators yourself -- but don't do that, use fullfile() instead.
5 Comments
Walter Roberson
on 4 May 2017
You
cd(handles.Dir.Functions_Dir)
and later
cd ..
Are we certain that cvv.m is one directory level above Functions_Dir ? Let us look:
handles.Dir.Home_Dir = [handles.Dir.Present_Dir 'cvv\']; % Home Directory
handles.Dir.Functions_Dir = [ handles.Dir.Home_Dir 'Functions\' ]; %Functions Directory
One level above Functions_Dir is going to be Home_Dir, which is a directory named cvv . It is unlikely in MS Windows that you would be using both a directory named cvv and a file named cvv.m in the same directory..
More Answers (0)
See Also
Categories
Find more on 迁移使用 GUIDE 创建的 App 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!