I am facing a problem in saving values from table to .mat file in app designer.
2 次查看(过去 30 天)
显示 更早的评论
I have to trees with two-two nodes, and one table is linked to the tree. When I select the nodes first, it should show the values taken from a .mat file, in the table. I want to change some values of the nodes and save them in the same .mat file. It is saving different values to the nodes automatically, and sometimes I am getting an error "Dot indexing is not supported for variables of this type''. Below find the code of the app designer.
properties (Access = private)
tab
Dx
Dy
Dz
IXX
IYY
IZZ
end
% Callbacks that handle component events
methods (Access = private)
% Selection changed function: Tree
function TreeSelectionChanged(app, event)
selectedNodes = app.Tree.SelectedNodes;
app.tab = load('property.mat');
if strcmp(selectedNodes.Text,'Center of Mass')
app.UITable.Data(1,1) = app.tab.dx(1);
app.UITable.Data(2,1) = app.tab.dy(1);
app.UITable.Data(3,1) = app.tab.dz(1);
end
if strcmp(selectedNodes.Text,'Inertia')
app.UITable.Data(1,1) = app.tab.Ixx(1);
app.UITable.Data(2,1) = app.tab.Iyy(1);
app.UITable.Data(3,1) = app.tab.Izz(1);
end
end
% Selection changed function: Tree_2
function Tree_2SelectionChanged(app, event)
selectedNodes2 = app.Tree_2.SelectedNodes;
app.tab = load('property.mat');
if strcmp(selectedNodes2.Text,'Center of Mass2')
app.UITable.Data(1,1) = app.tab.dx(2);
app.UITable.Data(2,1) = app.tab.dy(2);
app.UITable.Data(3,1) = app.tab.dz(2);
end
if strcmp(selectedNodes2.Text,'Inertia2')
app.UITable.Data(1,1) = app.tab.Ixx(2);
app.UITable.Data(2,1) = app.tab.Iyy(2);
app.UITable.Data(3,1) = app.tab.Izz(2);
end
end
% Cell edit callback: UITable
function UITableCellEdit(app, event)
selectedNodes = app.Tree.SelectedNodes;
app.tab = load('property.mat');
if strcmp(selectedNodes.Text,'Center of Mass')
app.Dx = app.UITable.Data(1,1);
app.Dy = app.UITable.Data(2,1);
app.Dz = app.UITable.Data(3,1);
x1 = app.Dx;
y1 = app.Dy;
z1 = app.Dz;
dx = [x1,app.tab.dx(2)];
dy = [y1,app.tab.dx(2)];
dz = [z1,app.tab.dx(2)];
Ixx = app.tab.Ixx;
Iyy = app.tab.Iyy;
Izz = app.tab.Izz;
save('property.mat','dx','dy','dz','Ixx','Iyy','Izz');
end
if strcmp(selectedNodes.Text,'Inertia')
app.IXX = app.UITable.Data(1,1);
app.IYY = app.UITable.Data(2,1);
app.IZZ = app.UITable.Data(3,1);
dx = app.tab.dx;
dy = app.tab.dy;
dz = app.tab.dz;
Ixx1 = app.IXX;
Iyy1 = app.IYY;
Izz1 = app.IZZ;
Ixx = [Ixx1,app.tab.Ixx(2)];
Iyy = [Iyy1,app.tab.Iyy(2)];
Izz = [Izz1,app.tab.Izz(2)];
save('property.mat','dx','dy','dz','Ixx','Iyy','Izz');
end
selectedNodes2 = app.Tree_2.SelectedNodes;
app.tab = load('property.mat');
if strcmp(selectedNodes2.Text,'Center of Mass2')
app.Dx = app.UITable.Data(1,1);
app.Dy = app.UITable.Data(2,1);
app.Dz = app.UITable.Data(3,1);
x2 = app.Dx;
y2 = app.Dy;
z2 = app.Dz;
dx = [app.tab.dx(1),x2];
dy = [app.tab.dx(1),y2];
dz = [app.tab.dx(1),z2];
Ixx = app.tab.Ixx;
Iyy = app.tab.Iyy;
Izz = app.tab.Izz;
save('property.mat','dx','dy','dz','Ixx','Iyy','Izz');
end
if strcmp(selectedNodes2.Text,'Inertia2')
app.IXX = app.UITable.Data(1,1);
app.IYY = app.UITable.Data(2,1);
app.IZZ = app.UITable.Data(3,1);
dx = app.tab.dx;
dy = app.tab.dy;
dz = app.tab.dz;
Ixx2 = app.IXX;
Iyy2 = app.IYY;
Izz2 = app.IZZ;
Ixx = [app.tab.Ixx(1),Ixx2];
Iyy = [app.tab.Iyy(1),Iyy2];
Izz = [app.tab.Izz(1),Izz2];
save('property.mat','dx','dy','dz','Ixx','Iyy','Izz');
end
end
end
4 个评论
KALYAN ACHARJYA
2020-9-30
Sir, I am unable to test the code due to the new version you are using. Hopefully you will get the response from other members. You can formulate the question with small example. In the RHS is the table, which is the different than listbox, right?
app.name1ListBox.Items=string_list_form_excel;
采纳的回答
Mario Malic
2020-9-30
编辑:Mario Malic
2020-9-30
Error is in callback UITableCellEdit, because, these are two different nodes and in each tree, there is always one active. So, if you click on second tree and want to update CoM2, the app will also update the values you input to the table to the node that is active on first tree, therefore it's a flaw.
selectedNodes = app.Tree.SelectedNodes;
if strcmp(selectedNodes.Text,'Center of Mass')
if strcmp(selectedNodes.Text,'Inertia')
selectedNodes2 = app.Tree_2.SelectedNodes;
if strcmp(selectedNodes2.Text,'Center of Mass2')
if strcmp(selectedNodes2.Text,'Inertia2')
You can create all four properties in the same tree, that would solve the issue.
Also, you can use a table with four rows and three columns (one for name of property and two for values for each model)
Also, you can use a structure, but it's a bit more complicated solution, better if you have many models that you want to calculate properties for
% This is just an idea, ideally you would have structure S.Model_1.dx, ...
S.Property = {'dx';'dy';'dz'; 'Ixx'; 'Iyy'; 'Izz'};
S.Model_1 = {4; 2; 3; 1; 2; 3;}
S.Model_2 = {1; 2; 4; 1; 4; 3;}
T = struct2table(S)
app.UITable2.Data = T
% You can create a callback that would update the values in fields and save structure
Regarding the last thread, callback won't be executed if the value did not change.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!