Pop-up Menu Issue

I have a pop-up menu having two options:
  1. Get message from user
  2. Upload message from file
The 2nd option is working fine but when I choose 1st option it's giving error:
Reference to non-existent field 'message'.
Error in mainmain>Embed_Callback (line 201)
message=handles.message;
The pop-up buttons function is as below:
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
Contents =cellstr(get(hObject,'String'));
pop_choice=Contents(get(hObject,'Value'));
pop_choice;
if (strcmp(pop_choice,'Enter Message'))
message=get(handles.EnterMessage,'string');
handles.message=message;
elseif (strcmp(pop_choice,'Upload Message'))
[file,Path]=uigetfile('*.txt','Select file to read message,'.')
message = fileread(fullfile(Path,file));
handles.message=message;
end
guidata(hObject,handles)

11 个评论

Rik
Rik 2019-7-26
编辑:Rik 2019-7-26
Please use the layout tools to make your code more readable.
Did you step through your code to find if either condition actually runs? Putting an else at the end that returns an understandable error may help you catch unexpected behavior.
No dear I haven't try the else one.
But both buttons have exactly same code then why it's giving error "Reference to non-existent field 'message'" to one button and not for the other?
The only possibility is that the field has not been set, so the code you intended to run for the second option is not actually running. Because you didn't share the code that sets the selection text, we have no way to know if you made a typo in either location.
This would probably be prevented by using the value property, instead of comparing to a string. Now you need to change two locations if you ever decide to use a different text.
I am calling the message which I select from the popup button in another function using:
message=handles.message;
Sorry I am new to the GUI so I have no depth idea :)
Can you please help me doing using value property? Or which code I need to share?
You can use this line to retrieve the value property. It will be an integer describing the selected option.
get(hObject,'Value')
You just need to make sure the callback has run, so the message field has been set and has been stored to the guidata.
Another option is to set a default message in the openFcn.
I have checked and came to know that other buttons except uploading button are not working. Although I am copying the same code of uploading button into another, that's not working.
Callback functions can be debugged like any other function. Just put a breakpoint at the first line and go through the code step by step.
Thank you I have done using the value method but now the issue is the string is not decoding properly. May be I need to get the input in char form? Is there any way to get input from user in char form instead of a string?
The string property contains a char array, not a string. This confusion was created when Mathworks introduced strings as a new data type, but (luckily) didn't rename properties or change their data types. That means both paths in your code should lead to char arrays.
So now the question is: have you made sure the message field contains a non-empty char array? And what do you mean with decoding?
Oh thanks for the clarification. Actually I am working on steganography so when I am embedding message using the uploading file I can extract the message properly but when I am getting input from user and then extracting the message it’s giving garbage value. Might be the field is not getting the user input. Making clear that both is working fine in editor and creating issue on GUI only.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Simulink Environment Customization 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by