GUI by code dynamic
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Elando
el 20 de Mayo de 2016
Hi,
I'm a beginner in creating GUI by code and I want to create something dinamic. I've create a excel file where I put all the property of the object that I have to plot in the GUI and then by code I create them because at the end they are going to be several object and I don't want to create il all manually one by one.
In the code I have put a condition that recognize the type of object and plot it with his specific property, but I can't make the same with the parent option. What do I have to put where there are the ???
Thank you Here is the code and the excel table:
switch Values{g,ind(2)} % rileva la tipologia dell'oggetto
case 'figure'
eval([Values{g,ind(3)} '= figure(''Name'',Values{g,ind(5)},''Position'',[Values{g,ind(6)} Values{g,ind(7)} Values{g,ind(8)} Values{g,ind(9)}])'])
case 'tab_group'
eval([Values{g,ind(3)} '= uitabgroup(''Parent'', ???,''Position'',[Values{g,ind(6)} Values{g,ind(7)} Values{g,ind(8)} Values{g,ind(9)}])'])
end

1 comentario
Stephen23
el 23 de Mayo de 2016
Using eval to access these variables is going to be very buggy and slow to run, as has been explained many times on this forum:
Respuesta aceptada
Stephen23
el 23 de Mayo de 2016
Editada: Stephen23
el 23 de Mayo de 2016
This is a classic example of how using eval makes code so obfuscated. Beginners often seem to think that using eval solves problems, but it really just makes the code hard to follow, buggy, and slow to run. Time to do some reading about eval:
The basic problem is: how to define the parent figure handle when the parent figure handle is dynamically defined? The best solution is: don't!
Here is a working example of how to use a structure to achieve something like what you seem to want, without using evil eval at all:
% read file data:
[~,~,raw] = xlsread('temp.csv');
% convert to structure:
C = raw(1,:);
C(2,:) = num2cell(raw(2:end,:),1);
S = struct(C{:});
% create graphics objects:
for k = 1:numel(S)
pos = [S(k).x,S(k).y,S(k).w,S(k).h];
S(k).position = pos;
switch S(k).type
case 'figure'
S(k).handle = figure('Name',S(k).name,...
'Units',S(k).units, 'Position',pos);
case 'uipanel'
S(k).handle = uipanel('Parent',S(1).handle,...
'Units',S(k).units, 'Position',pos);
otherwise
error('Sorry, but %s is not supported.',S(k).type)
end
end
You will find all of the file data and handles clearly listed in the structure S: because a structure is used each field can be easily accessed using its fieldname (which is much more robust than using indexing into a cell array). This also means that you can change the order of the columns in the file, or add new columns of information if you wish to, and the code will keep working, as long as the fieldnames are the same.
You can also access the field values of the structure very easily:
Note that in this example I have just one figure and defined it first: if you have multiple figures or nested graphics objects then you will need to manage those handles in some way to refer to them and track them correctly. Such a scheme is beyond the scope of this question (or my answer) because it depends entirely on how complicated your UI will be. Something to think about though.
By the way, my code creates this figure:

Because you did not provide us with any sample data file I created my own text file with some figure parameters. You can find it here:
2 comentarios
Más respuestas (1)
Geoff Hayes
el 23 de Mayo de 2016
Elando - using eval is generally discouraged as it leads to code that is difficult to trouble shoot whenever a bug arises. In this case, I understand why you are doing it this way as you are trying to build a GUI from an Excel file that you can presumably make changes to and then run your code that will build the newly designed GUI.
So, the parent of your tgroup is Main_fig, or Values{g,ind(4) which you should be able to use in place of the ??? in the above code.
1 comentario
Ver también
Categorías
Más información sobre Data Import from MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!