Dynamically create new axes and associated callbacks in GUIDE

12 visualizzazioni (ultimi 30 giorni)
I made a GUI using GUIDE that plays multiple videos. Each video is displayed in an axes which was created in GUIDE. However, I need to dynamically create axes depending on the number of videos that the user selects.
My initial hack is to create a single axes in GUIDE and set the Visible attribute to 'off'. Then on the Callback from the filebrowser make a bunch of copies of the axes:
for idx = 1:num_videos
eval(['handles.axes' num2str(idx) ' = handles.axes1']);
% set size, location, tags, etc.
end
Then in a second loop, set the visibility:
for idx = 1:num_videos
eval(['handles.axes' num2str(idx) '.Visible = ''on''']);
end
However, I'm really looking for a better way to do this. Can I just initialize an object of the axes class and set the attributes that way and how can I do it without using eval? Also, would the Parent be the handles object itself or the base Figure itself (matlab.ui.Figure)?
Secondly, if I want to (dynamically) create an associated button, how can I dynamically add callback functions? Using GUIDE, it creates a separate Callback for each button based on the tag, but if I'm creating a button in the .m file what is the best way to accomplish this?
  3 Commenti
Stephen23
Stephen23 il 6 Apr 2020
Ugh, do NOT use eval for trivial code like this. Instead you should use better dynamic fieldnames:
handles.(sprintf('axes%d',idx)) = ...
Or avoid the entire complexity by using a much simpler and more efficient array of handles and indexing:
handles.myaxes = gobjects(N,1);
...
handles.myaxes(idx) = ...
matquest
matquest il 6 Apr 2020
@Geoff Hayes That's correct - I need the videos to play simultaneously. I'm setting the inital dimensions of the GUI to 750x700 (though it's resizeable). I'm going to assume that the user likely won't be resizing the GUI and so set the location of each axes based on the initial size and number of videos.
@Stehen Cobeldick - thank you for the suggestions. I am not very familiar with matlab/matlab best practices, but I suspected that using eval was probably not the right approach. I will probably go with the array of handles in this case.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by