フィルターのクリア

How to index tables in a struct? (to call each field from App Designer)

6 ビュー (過去 30 日間)
yvesvincent abgrall
yvesvincent abgrall 2019 年 9 月 6 日
編集済み: Stephen23 2019 年 9 月 6 日
Hello,
I'm facing a problem while coding on App Designer. I have saved a workspace made of diferent tables.
The tables are created this way:
a{1}=
a{2}=
a{3}=
etc...
I created it this way to be able to call them in the code on App Designer (in which I have imported the workspace with the command "load") in a loop, for example:
app.data = load('workspace.mat'); % command used to load the workspace in App Designer
for i = 1 : numel(fieldnames(app.data))
disp(app.data.a(i)) %here is the problem
end
Matlab send me an error when executing this or any combination of app.data.a with (i) or {i} ...
A "a" field of cell type is beeing created when running the workspace but this same "a" clas isn't loaded with the rest of the fields when importing in App Designer.
Thanking you in advance for your help :)
  7 件のコメント
yvesvincent abgrall
yvesvincent abgrall 2019 年 9 月 6 日
Hello Nicolas,
this is the message i get when doing so:
Reference to non-existent field 'GT'.
Error in app1/SelectionListBoxValueChanged (line 28)
disp(app.data.GT{15});
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 378)
Error while evaluating ListBox PrivateValueChangedFcn.
Stephen23
Stephen23 2019 年 9 月 6 日
編集済み: Stephen23 2019 年 9 月 6 日
@yvesvincent abgrall your description and your actual data are very different. What you showed in your question uses indexing into a cell array or table, e.g.:
"The tables are created this way:"
a{1}=
a{2}=
a{3}=
etc...
But your response to my request for information shows that in fact you have a whole lot of fields, and indexing is not involved at all. You should revise basic MATLAB concepts, e.g. what is indexing, what are structure fields, etc. as you have mixed them up and provided conflicting information on this thread.
"I started indexing at GT{14}"
In fact you don't, because indices are totally unrelated to fieldnames. Do not confuse them.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2019 年 9 月 6 日
編集済み: Stephen23 2019 年 9 月 6 日
You need to loop over the fieldnames:
app.data = load('workspace.mat');
C = fieldnames(app.data);
for k = 1:numel(C)
T = app.data.(C{k});
... do whatever with table T
end
Read this:
  1 件のコメント
yvesvincent abgrall
yvesvincent abgrall 2019 年 9 月 6 日
Perfect it's working!
Thank you all for your help :)

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2019 年 9 月 6 日
app.data = load('workspace.mat'); % command used to load the workspace in App Designer
for i = 1 : numel(fieldnames(app.data))
disp(app.data.a(i)) %here is the problem
end
This looks suspicious. You're iterating from 1 to the number of fields in app.data, but then you're not using the fieldnames to access the data. I would expect you to use the list of fieldnames in conjunction with dynamic field names.
app.data = load('workspace.mat'); % command used to load the workspace in App Designer
FN = fieldnames(app.data);
for i = 1 : numel(FN)
disp(app.data.(FN{i})) %here is the problem
end
If you need to further index into a table that is one of the fields in app.data, use one of the types of indexing shown on this documentation page. Note in particular that if you're using parentheses or curly braces, you need to specify two indices. Asking for T(1) of a table won't work, but asking for T(1, 1) or T(1, :) of that table would.

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by