AppDesigner How do you load a .mat table and refer to its variables
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I started using appDesigner, but do not see how to refer to a .mat table's variables. My import includes the following:
properties (Access = public)
wdf_c_tr % Description
end
% Code that executes after component creation
function startupFcn(app)
wdf_c_tr = load('combined_altered.mat');
I am not sure if I need to put "app." prior to the table name ( app.wdf_c_tr ...) in the startupFcn section.
wdf_c_tr is a table with about 80 variables. I want to select rows based on 'sync' and 'GBS', and create a swarm chart of a measurement (selected by the user) vs categories in a wdf_c_tr variable (selected by the user).
idx = ( ( wdf_c_tr.sync == "abx" | wdf_c_tr.sync == "cdx" ) & wdf_c_tr.GBS == 0 );
yval = app.MeasurementDropDown.Value;
xval = app.ListBox.Value;
y = swarmchart( wdf_c_tr.(xval)(idx) , wdf_c_tr.(yval)(idx) );
I run this in matlab successfully:
idx = ( ( wdf_c_tr.sync == "abx" | wdf_c_tr.sync == "cdx" ) & wdf_c_tr.GBS == 0 );
figure;
y = swarmchart( wdf_c_tr.elevation(idx) , wdf_c_tr.measure1(idx) );
I get the error sync is unrecognized. So I found some online videos, and revised to
idx = ( ( wdf_c_tr(:,"sync") == "abx" | wdf_c_tr(:,"sync") == "cdx" ) & wdf_c_tr(:,"GBS") == 0 );
but I get the error:
Unable to use a value of type string as an index.
Any recommendations are appreciated.
I was hoping matlab and appdesigner code would be equivalent, but am thinking it is not.
And many tutorials do not seem to describe how to create a user interface that would allow a user to slice and dice a large data set. Most of it is 'hello world', calculators, and graphing sine waves. Any tutorial recommendations are appreciated.
I can post a snippet of the table if needed. It is very large though.
0 comentarios
Respuesta aceptada
Voss
el 27 de Dic. de 2022
I think the problem is that wdf_c_tr is not a table; rather it is a struct with a field that is the table.
Consider:
% a table:
t = table([1;2;3],[4;5;6]);
% save the table to a mat file:
save('mat_file.mat','t');
% now load the mat file:
t = load('mat_file.mat')
See? t = load(...) makes t a struct, not a table. t.t is the table:
t.t
So, in your case you can do something like this in your startupFcn:
temp = load('combined_altered.mat');
app.wdf_c_tr = temp.wdf_c_tr;
And yes, you need the "app." in front if you want to be able to use that table outside the startupFcn, which I imagine you do. And you would refer to it as "app.wdf_c_tr" everywhere in your code (except where it is declared as a property - that part is ok), e.g.:
idx = ( ( app.wdf_c_tr.sync == "abx" | app.wdf_c_tr.sync == "cdx" ) & app.wdf_c_tr.GBS == 0 );
yval = app.MeasurementDropDown.Value;
xval = app.ListBox.Value;
y = swarmchart( app.wdf_c_tr.(xval)(idx) , app.wdf_c_tr.(yval)(idx) );
2 comentarios
Más respuestas (1)
dpb
el 27 de Dic. de 2022
Editada: dpb
el 27 de Dic. de 2022
The problem is in the form of load you used with a LHS assignment variable -- that creates a struct containing the data in the .mat file. See load for the details on syntax.
What you want is to simply load the table back into the app workspace just as you would from the command line -- if you're certain the input file is never going to change names since you hardcoded it into the source, then just use
load 'combined_altered.mat'
and the variable(s) that you SAVE'd into the .mat file when it was created are "poofed" back into the workspace with the same names and characteristics they had before. Then all you other addressing of the table variable will work just as it did in the workspace/command line.
I'd strongly recomend to make the input file a user-selected filename, however, then the load function would have to revert to the functional form but again, don't use the LHS assignment--
load(app.UserInputFullyQualifiedFileName,'wdf_c_tr')
will leave you in the same place with the given variable name but from a filename selected with uigetfile or some other UI manner.
I'd suggest changing up a little and make the variable name in the code generic and short, however, for convenience.
Also, in the other addressing code, you can use more sophisticated addressing expressions with variables for variable names and be able to write more complex logic in simpler fashion.
For such things as this, having a shortish subset of the pertinent variables in the table would probably help in getting more specific answers...
0 comentarios
Ver también
Categorías
Más información sobre Data Type Identification 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!