Matlab - Actualizar Uitable en una GUI hecha en Matlab

 
Vista:
sin imagen de perfil

Actualizar Uitable en una GUI hecha en Matlab

Publicado por Hector (15 intervenciones) el 29/01/2014 02:07:02
Estimados,

estoy intentando almacenar en una uitable datos provenientes de unos cálculos hechos en una gui en matlab, y estoy atascado al querer actualizar el nombre de las filas. Hasta ahora he podido "guardar" los datos en sus correspondientes columnas, pero no comprendo bien como hacerlo para el caso de las filas. Por otra parte me gustaría que me ayudaréis en actualizar los datos en esta uitable en el caso de que haga múltiples cálculos, y pudiendo actualizar la tabla al agregar dichos valores.

Adjunto mi código agradeciendo enormemente vuestra ayuda.

Saludos cordiales

Héctor


================================== Código ===============================

function Gui_Table (varargin)

close all
clear all
clc

panel_color=[1 1 1];

entryField_color=[0.51 0.51 0.51];


cnames = {'Addition','Substraction','Division','Product'};
%rnames = {'First','Second','Third','Fourth','Fifth'};

hFigure= figure('MenuBar','none',...
'Units','Pixels',...
'Color','w',...
'Toolbar','none',...
'Name','Table model',...
'resize','off',...
'NumberTitle','off',...
'Position',[1 810 1280 683]);

movegui(hFigure,'center');

warning off MATLAB:uitabgroup:OldVersion % turn off warning

hg=uitabgroup('Parent',hFigure,...
'units','pixels','position',...
[1.5 10 1277 677]);

%=====================================================Main Tabs==========%
ht(2)=uitab(hg,'Title','My Table');
%=====================================================Main Panel=========%

hPanelB = uipanel(...
'Title','',...
'Fontsize',22,...
'Parent', ht(2),...
'Units','Pixels',...
'BorderWidth',1,...
'Position',[1 1 1263 640],...
'BackgroundColor',panel_color);

%---------------------------------------------------Sub_Panels--------------------------------------------------------------

%## Sub Panels ##%

hSubPanel1 = uipanel('Parent',ht(2),'Units',...
'pixels','Title','Input Parameters','Fontsize',18,...
'BackgroundColor',[1 1 1],'Position',[5.5 194 595 133],...
'BorderWidth',2);

hSubPanel2 = uipanel('Parent',hPanelB,'Units',...
'pixels','Title','Main table','Fontsize',18,...
'BackgroundColor',[1 1 1],'Position',[620 70 620 350],...
'BorderWidth',2);

hSubPanel3 = uipanel('Parent',hPanelB,'Units',...
'pixels','Title','Results','Fontsize',18,...
'BackgroundColor',[1 1 1],'Position',[620 450 620 170],...
'BorderWidth',2);

%================================== Ui Table =====================

t = uitable('Parent',hSubPanel2,'ColumnName',cnames,...
'Position',[7 10 600 300]);

set(t,'ColumnWidth',{75})

%================================== Input (User data) =====================

%## ID
h1label = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.06 0.70 0.17 0.14],'String','Part ID:','Style','Text');

h1Out = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.175 0.70 0.30 0.22],...
'String',{''},'value',0,'Style','edit');

%## Number 1

h2label = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.51 0.70 0.15 0.17],'String','Number 1:','Style','text');

h2Out = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.66 0.70 0.30 0.22],...
'String',{''},'value',0,'Style','edit');

%## Number 2

h3label = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.04 0.40 0.15 0.17],'String','Number 2:','Style','text');

h3Out = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.18 0.39 0.30 0.22],...
'String',{''},'value',0,'Style','edit');

%## Number 3

h4label = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.51 0.40 0.15 0.17],'String','Number 3:','Style','text');

h4Out = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.66 0.39 0.30 0.22],...
'String',{''},'value',0,'Style','edit');


%================================== Output (Analysis) =====================

%## Addition

h5label = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.01 0.65 0.15 0.17],'String','Addition:','Style','text');

h5Out = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.13 0.64 0.30 0.22],...
'String',{'N1+N2+N3'},'value',0,'Style','edit');

%## Substraction

h6label = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.51 0.65 0.17 0.17],'String','Substraction:','Style','text');

h6Out = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.69 0.64 0.30 0.22],...
'String',{'N1-N2-N3'},'value',0,'Style','edit');

%## Division

h7label = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.01 0.35 0.15 0.17],'String','Division:','Style','text');

h7Out = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.13 0.34 0.30 0.22],...
'String',{'N1/N2/N3'},'value',0,'Style','edit');

%## Product

h8label = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.57 0.35 0.17 0.17],'String','Product:','Style','text');

h8Out = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.69 0.34 0.30 0.22],...
'String',{'N1*N2*N3'},'value',0,'Style','edit');

%######## Analysis Push Buttons---

hMainPushButton = uicontrol('Parent',hSubPanel1,'Units',...
'normalized','Position',[0.78 0.01 0.22 0.18],'Style','Pushbutton',....
'String',' Run!','FontSize',20,'Callback',@MainPushButton_Callback);

hSavePushButton = uicontrol('Parent',hSubPanel3,'Units',...
'normalized','Position',[0.32 0.01 0.22 0.28],'Style','Pushbutton',....
'String',' Save!','FontSize',20,'Callback',@SavePushButton_Callback);

hResetPushButton = uicontrol('Parent',hSubPanel3,'Units',...
'normalized','Position',[0.54 0.01 0.22 0.28],'Style','Pushbutton',....
'String',' Reset!','FontSize',20,'Callback',@ResetPushButton_Callback);

%######## Handle List ---

handle_list=...
[hFigure,hg,ht(2),hSubPanel1,...
hSubPanel2,hSubPanel3,t,h1label,h1Out,...
h2label,h2Out,h3label,h3Out,h4label,...
h4Out,h5label,h5Out,h6label,h6Out,.....
h7label,h7Out,h8label,h8Out,.....
hMainPushButton,hSavePushButton,...
hResetPushButton];
set(handle_list,'Units','normalized');

%------------------------------------------------------- EVALUATION FUNCTION BEGINS /Callbacks -------------------------------

% % --- Executes on button press in run_rad_cal.
function MainPushButton_Callback(~,~)

N1 = str2double(get(h2Out,'string')); %Number 1
N2 = str2double(get(h3Out,'string')); %Number 2
N3 = str2double(get(h4Out,'string')); %Number 3

Add = (N1 + N2 + N3);

Sub = (N1 - N2 - N3);

Div = (N1 ./ N2 ./ N3);

Pro = (N1 .* N2 .* N3);

set(h5Out,'string',num2str(Add));
set(h6Out,'string',num2str(Sub));
set(h7Out,'string',num2str(Div));
set(h8Out,'string',num2str(Pro));

end

function SavePushButton_Callback(~,~)

data{1,1}= get(h5Out,'string');
data{1,2}= get(h6Out,'string');
data{1,3}= get(h7Out,'string');
data{1,4}= get(h8Out,'string');
set(t,'data',data);

end

function ResetPushButton_Callback(~,~)

set(t,'Data',[])

end

%------------------------------------------------------- EVALUATION FUNCTION ENDS -------------------------------


end
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Actualizar Uitable en una GUI hecha en Matlab

Publicado por Hector (15 intervenciones) el 01/02/2014 19:18:05
Estimados,

he podido actualizar los datos de las columnas, pero lo único que no he podido hacer, es actualizar el nombre de la fila. Este nombre de fila se introduce manualmente a través de la gui en el edit box "Part ID", y finalmente se muestra en la tabla. Cada vez que hago un cálculo nuevo, lo introduce en la tabla, pero no me cambia el nombre de la fila, es decir, solo introduce el nombre de la fila en el primer cálculo.

Incluyo el código con las actualizaciones agradeciendo infinitamente vuestra ayuda.

Saludos cordiales

Héctor

================================== Desde aquí el código ======================================

function Gui_Table (varargin)

close all
clear all
clc

panel_color=[1 1 1];

entryField_color=[0.51 0.51 0.51];


cnames = {'Addition','Substraction','Division','Product'};

hFigure= figure('MenuBar','none',...
'Units','Pixels',...
'Color','w',...
'Toolbar','none',...
'Name','Table model',...
'resize','off',...
'NumberTitle','off',...
'Position',[1 810 1280 683]);

movegui(hFigure,'center');

warning off MATLAB:uitabgroup:OldVersion % turn off warning

hg=uitabgroup('Parent',hFigure,...
'units','pixels','position',...
[1.5 10 1277 677]);

%=====================================================Main Tabs==========%
ht(2)=uitab(hg,'Title','My Table');
%=====================================================Main Panel=========%

hPanelB = uipanel(...
'Title','',...
'Fontsize',22,...
'Parent', ht(2),...
'Units','Pixels',...
'BorderWidth',1,...
'Position',[1 1 1263 640],...
'BackgroundColor',panel_color);

%---------------------------------------------------Sub_Panels--------------------------------------------------------------

%## Sub Panels ##%

hSubPanel1 = uipanel('Parent',ht(2),'Units',...
'pixels','Title','Input Parameters','Fontsize',18,...
'BackgroundColor',[1 1 1],'Position',[5.5 194 595 133],...
'BorderWidth',2);

hSubPanel2 = uipanel('Parent',hPanelB,'Units',...
'pixels','Title','Main table','Fontsize',18,...
'BackgroundColor',[1 1 1],'Position',[620 70 620 350],...
'BorderWidth',2);

hSubPanel3 = uipanel('Parent',hPanelB,'Units',...
'pixels','Title','Results','Fontsize',18,...
'BackgroundColor',[1 1 1],'Position',[620 450 620 170],...
'BorderWidth',2);

%================================== Ui Table =====================

t = uitable('Parent',hSubPanel2,'ColumnName',cnames,...
'Position',[7 10 600 300], 'CellEditCallback',@SavePushButton_Callback);

set(t,'ColumnWidth',{142})

%================================== Input (User data) =====================

%## ID
h1label = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.06 0.70 0.17 0.14],'String','Part ID:','Style','Text');

h1Out = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.175 0.70 0.30 0.22],...
'String',{''},'value',0,'Style','edit');

%## Number 1

h2label = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.51 0.70 0.15 0.17],'String','Number 1:','Style','text');

h2Out = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.66 0.70 0.30 0.22],...
'String',{''},'value',0,'Style','edit');

%## Number 2

h3label = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.04 0.40 0.15 0.17],'String','Number 2:','Style','text');

h3Out = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.18 0.39 0.30 0.22],...
'String',{''},'value',0,'Style','edit');

%## Number 3

h4label = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.51 0.40 0.15 0.17],'String','Number 3:','Style','text');

h4Out = uicontrol('Parent',hSubPanel1 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.66 0.39 0.30 0.22],...
'String',{''},'value',0,'Style','edit');


%================================== Output (Analysis) =====================

%## Addition

h5label = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.01 0.65 0.15 0.17],'String','Addition:','Style','text');

h5Out = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.13 0.64 0.30 0.22],...
'String',{'N1+N2+N3'},'value',0,'Style','edit');

%## Substraction

h6label = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.51 0.65 0.17 0.17],'String','Substraction:','Style','text');

h6Out = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.69 0.64 0.30 0.22],...
'String',{'N1-N2-N3'},'value',0,'Style','edit');

%## Division

h7label = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.01 0.35 0.15 0.17],'String','Division:','Style','text');

h7Out = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.13 0.34 0.30 0.22],...
'String',{'N1/N2/N3'},'value',0,'Style','edit');

%## Product

h8label = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',[1 1 1],'FontSize',17,...
'ForegroundColor',[0 0 0],'HorizontalAlignment','left',...
'Position',[0.57 0.35 0.17 0.17],'String','Product:','Style','text');

h8Out = uicontrol('Parent',hSubPanel3 ,'Units',...
'normalized','BackgroundColor',entryField_color,'FontSize',16.5,...
'ForegroundColor',[1 1 0],'Position',[0.69 0.34 0.30 0.22],...
'String',{'N1*N2*N3'},'value',0,'Style','edit');

%######## Analysis Push Buttons---

hMainPushButton = uicontrol('Parent',hSubPanel1,'Units',...
'normalized','Position',[0.78 0.01 0.22 0.18],'Style','Pushbutton',....
'String',' Run!','FontSize',20,'Callback',@MainPushButton_Callback);

hSavePushButton = uicontrol('Parent',hSubPanel3,'Units',...
'normalized','Position',[0.32 0.01 0.22 0.28],'Style','Pushbutton',....
'String',' Save!','FontSize',20,'Callback',@SavePushButton_Callback);

hResetPushButton = uicontrol('Parent',hSubPanel3,'Units',...
'normalized','Position',[0.54 0.01 0.22 0.28],'Style','Pushbutton',....
'String',' Reset!','FontSize',20,'Callback',@ResetPushButton_Callback);

hExportPushButton = uicontrol('Parent',hSubPanel2,'Units',...
'normalized','Position',[0.76 0.04 0.22 0.20],'Style','Pushbutton',....
'String',' Export!','FontSize',20,'Callback',@ExportPushButton_Callback);


%######## Handle List ---

handle_list=...
[hFigure,hg,ht(2),hSubPanel1,...
hSubPanel2,hSubPanel3,t,h1label,h1Out,...
h2label,h2Out,h3label,h3Out,h4label,...
h4Out,h5label,h5Out,h6label,h6Out,.....
h7label,h7Out,h8label,h8Out,.....
hMainPushButton,hSavePushButton,...
hResetPushButton,hExportPushButton];
set(handle_list,'Units','normalized');

%------------------------------------------------------- EVALUATION FUNCTION BEGINS /Callbacks -------------------------------

% % --- Executes on button press in run_rad_cal.
function MainPushButton_Callback(~,~)

N1 = str2double(get(h2Out,'string')); %Number 1
N2 = str2double(get(h3Out,'string')); %Number 2
N3 = str2double(get(h4Out,'string')); %Number 3

Add = (N1 + N2 + N3);

Sub = (N1 - N2 - N3);

Div = (N1 ./ N2 ./ N3);

Pro = (N1 .* N2 .* N3);

set(h5Out,'string',num2str(Add));
set(h6Out,'string',num2str(Sub));
set(h7Out,'string',num2str(Div));
set(h8Out,'string',num2str(Pro));

end

function SavePushButton_Callback(~,~)

%%##
data{1,1}= get(h5Out,'string');
data{1,2}= get(h6Out,'string');
data{1,3}= get(h7Out,'string');
data{1,4}= get(h8Out,'string');


oldData = get(t,'Data');
newRow = cat(0,data,cell(0,size(data,2)));
newData2 = [oldData; newRow];
set(t,'Data',newData2);

backgroundColor = [.5 .41 .21; .31 .45 .64];


rowname = get(h1Out,'string');
set(t,'Rowname',rowname);
set(t, 'BackgroundColor', backgroundColor);
set(t, 'ColumnEditable', [true false false false]);

end


function ResetPushButton_Callback(~,~)
set(t,'Data',[])
set(t,'Rowname',[]);
end

function ExportPushButton_Callback(~,~)

C = get (t, 'Data');
n = size (C,1);
File = fopen('exporting.dat','w');

for i = 1:n
fprintf (File, '%s %s %s %s \n', C(i,:),str{1});
end

fclose(File);

end
%------------------------------------------------------- EVALUATION FUNCTION ENDS -------------------------------
end
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar