how to use function to output code to control GUIDE

In my GUIDE, user can adjust number and then the display item will change.
I have already wrote a code like this:
for i=1:maxnumber
k1=eval(['handles.TD',num2str(i),'X']);
k2=eval(['handles.TD',num2str(i),'Y']);
k3=eval(['handles.TD',num2str(i),'R']);
k4=eval(['handles.TD',num2str(i),'AMP']);
k5=eval(['handles.TD',num2str(i),'CD']);
set(k1,'visible','on');
set(k2,'visible','on');
set(k3,'visible','on');
set(k4,'visible','on');
set(k5,'visible','on');
end
The key is last four "set" statement.
Now I want to write a function so I don't need to type this every time.
The code for function is the same as above.
But how can I use function to output "set" statement to control GUIDE?
Also, I found out I cannot use handles in function....is there a solution?

 Réponse acceptée

Jan
Jan le 21 Mai 2017
Of course you can use handles in a function - when you provide it as input argument.
Do not use eval to address a field of a struct. This is such ugly and evil. See https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval and hundrets of corresponding discussions in this forum.
function MySetFcn(maxnumber, handles)
for i=1:maxnumber
H = [handles.(sprintf('TD%dX', i)), ...
handles.(sprintf('TD%dY', i)), ...
handles.(sprintf('TD%dR', i)), ...
handles.(sprintf('TD%dAMP', i)), ...
handles.(sprintf('TD%dCD', i))];
set(H,'visible','on');
end
Call this from your callbacks as:
MySetFcn(maxnumber, handles)
The key is last four "set" statement.
The key for what?
But how can I use function to output "set" statement to control GUIDE?
GUIDE is the tool to create a GUI. Do you want to control GUIDE or the GUI?
Also, I found out I cannot use handles in function....is there a solution?
Please explain this claim. Show your code and the error message.

4 commentaires

Mahdie Rezaeian
Mahdie Rezaeian le 21 Mai 2017
Modifié(e) : Mahdie Rezaeian le 21 Mai 2017
I have the same problem. What I want to do is plotting a column of an Excel file selected by the user from a popup menu. I've replicated the instructions in this video . the function for updating axes named "updateAxes". I tried calling this function in popup menu callback as follows:
set(handles.popupmenu1,'callback', '@(hObject,eventdata)mainGUI(''updateAxes'',hObject,eventdata,guidata(hObject))');
However, it's not working. how should I call updateAxes? I've attached the code and a sample excel sheet for your reference.
herb
herb le 21 Mai 2017
"The key is last four "set" statement." means I can type this inside my GUI function to control visible or not. And because I will do this same thing many times, I want to write it into a function.
But I don't know how to write a function to output "set(handles.GUI,'visible','on')" to do the same thing.
I used GUIDE to create GUI, so what I want to control is the GUI.
@Mahdie: What about:
set(handles.popupmenu1,'callback', ...
@(hObject,eventdata) updateAxes(hObject, eventdata, guidata(hObject)));
Hi Jan, Thanks for your reply. it works:) The other option is: set(handles.popupmenu1,'callback', 'mainGUI(''updateAxes'',gcbo,[],guidata(gcbo))')

Connectez-vous pour commenter.

Catégories

En savoir plus sur Debugging and Improving Code dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by