How to Transfer data from Main gui to another GUI in case , when multiple instances of GUI is used ?
    3 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
    Sundeepan
 il 29 Ago 2016
  
    
    
    
    
    Commentato: Sundeepan Sarkar
 il 30 Ago 2016
            I have a MATLAB GUI (MAIN GUI) which is used to run a simulation and the results are plotted in a separate GUI (Plotting GUI). I have used setappdata/getappdata meathod to transfer data between the GUI. Additionally user can have multiple instances of the plotting GUI (where results are plotted) to plot different signals. Though I have used setappdata to select the GCF (current figure) in the opening function of the Plotting GUI, the results are getting plotted in different GUI (ex. Results intended to be plotted on plotting GUI 2 are plotted in plotting GUI 1).
How can I ensure that the intended instace of the GUI is selected for plotting of results ?
2 Commenti
  Geoff Hayes
      
      
 il 29 Ago 2016
				Sundeepan - how exactly are you passing data to the plotting GUI so that it "knows" that it is the only one that should be updated and not the other ones? Please provide a small code sample that illustrates this problem.
  Sundeepan Sarkar
 il 29 Ago 2016
				
      Modificato: Sundeepan Sarkar
 il 29 Ago 2016
  
			In the MAIN GUI, multiple instance of the plotting GUI are called based on the user input.
%%MAIN GUI
%  User Defines number of figures required & value is saved in num
    for l = 1:num
    setappdata(0,'figureid',l);
    id = strcat('hPlot',num2str(l)); % name assigned to each figure 
    eval(sprintf('%s = PlottingGUI;',id));   % call plotting GUI 
    end
In opening function of the second GUI, the current figure is assigned with the figure ID. Then the Figure ID is used again in the Main GUI for plotting the results.
%%PLOTTING GUI
%  Plotting GUI is selected in the Opening Function 
    function plottingGUI_OpeningFcn(hObject,~,handles, varargin)
      handles.output  = hObject;
    figureid         = getappdata(0,'figureid');
    PlotID           = strcat('Plot',num2str(figureid));
    PlotFig          = strcat('Figure',num2str(figureid));
    eval(sprintf('%s = %s ;',PlotFig,gcf)); 
    eval(sprintf('setappdata(0 ,''%s'', %s);',PlotID,PlotFig)); % stores handle to this gui in root
In Main GUI, the plotting GUI is then called using the following code:
    for figure_number = 1:num 
    GenID = strcat('Plot',num2str(figure_number));
         GenGUI = getappdata(0,GenID);                  % collects handle to the plotting gui
         g_main = findobj(GenGUI , 'type' , 'axes');    % looks for object of type                                                             
         axes(g_main);                                  % axes in the second gui
Risposta accettata
  Geoff Hayes
      
      
 il 29 Ago 2016
        Sundeepan - I strongly suggest that you don't use eval to evaluate your expressions. Your
 id = strcat('hPlot',num2str(l)); % name assigned to each figure 
 eval(sprintf('%s = PlottingGUI;',id));   % call plotting GUI
seems to be dynamically creating local variables to the multiple instances of the PlottingGUI. But what happens to these variables afterwards? How do you reference them? A better solution would be to create an array of handles to the GUIs like
 hPlottingGui = [];
 for l = 1:num
     hPlottingGui(l) = PlottingGUI;
 end
With the above, you will still have all of the handles but they will be stored in an array and so easier to access/manipulate.
With your third piece of code, you state that In Main GUI, the plotting GUI is then called using the following code: which is
 for figure_number = 1:num 
    GenID = strcat('Plot',num2str(figure_number));
    GenGUI = getappdata(0,GenID);                  % collects handle to the plotting gui
    g_main = findobj(GenGUI , 'type' , 'axes');    % looks for object of type                                                             
    axes(g_main);
    % etc.
 end
But the above iterates over ALL of your Plotting GUIs and not just a particular one. This implies that all of them will be updated. So how do you know when the Main GUI should update the Plotting GUI k?
3 Commenti
  Geoff Hayes
      
      
 il 29 Ago 2016
				But your main GUI has the handles to all of the Plotting GUIs, so why isn't that sufficient? Presumably your Main GUI decides what signal should be drawn where.
As for the gcf should refer to the current GUI when referenced to in the Opening function, do you mean that the current GUI is the Plotting GUI that you just launched? If so, the handle for this would be hObject, the first input parameter into the OpeningFcn.
  Sundeepan Sarkar
 il 30 Ago 2016
				One of my team-mate proposed a solution to the problem :
Ideally the gcf should refer to the current GUI when referenced to in the Opening function. But for me it takes the pervious GUI as the reference, which is incorrect.
Apperently MATLAB required some time to refresh the gcf (current figure), so all I needed to do was to add a pause just before the Opening Function of the Plotting GUI.
This helps in correctly selcting the GUI figure as gcf.
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Graphics Objects in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


