Unrecognized Field name in struct error, even though same Fieldname clearly in struct

78 次查看(过去 30 天)
I am writing a GUI, that automatically imports data from a table and sorts it into a struct with fields. I created a cell with strings "VN" (VariableNames), with data_in beeing a 401x6 table in this example.
app.VN = app.data_in.Properties.VariableNames;
% Manually outputting VN into workspace shows:
VN=app.VN
VN =
1×6 cell array
Columns 1 through 5
{'x1_1_1RadVorneR…'} {'x1_1_1RadVorneR…'} {'x2_1_1Sitzschiene'} {'x2_1_1Sitzschie…'} {'x3_1_1Sitzkissen'}
Column 6
{'x3_1_1Sitzkisse…'}
so i can later open each field in my struct "data" by calling it like this for example:
app.data.(app.VN{i})
My Problem: I want to compare different Fields with eachother in an if statement, with "i" beeing used from a for function:
for i=2:width(app.data_in)
if app.data.(app.VN{1}) == app.data.(app.VN{i})
%...
But i get the warning message "Unrecognized field name "x1_1_1RadVorneRechts"." in the line of the if statement.
If I manually output the struct "data" into the workspace, one line before the aforementioned "for" function starts, i get this output, clearly showing, that the field name exists:
data=app.data
data =
struct with fields:
x1_1_1RadVorneRechts: [401×1 double]
x1_1_1RadVorneRechts_2: [401×1 double]
x2_1_1Sitzschiene: [401×1 double]
x2_1_1Sitzschiene_2: [401×1 double]
x3_1_1Sitzkissen: [401×1 double]
x3_1_1Sitzkissen_2: [401×1 double]
Why do i get this error message and how do i resolve it? It is critical for the rest of the GUI, that i can compare different field values with eachother.
  9 个评论
Janik Ruge
Janik Ruge 2021-8-21
Did that, i just paste the entire workspace result here:
datafields =
6×1 cell array
{'x1_1_1RadVorneRechts' }
{'x1_1_1RadVorneRechts_2'}
{'x2_1_1Sitzschiene' }
{'x2_1_1Sitzschiene_2' }
{'x3_1_1Sitzkissen' }
{'x3_1_1Sitzkissen_2' }
All VN data fields found
% Rest was in red letters, as per usual for an error msg
Unrecognized field name "x1_1_1RadVorneRechts".
Error in SS14/DateiAuswahl2 (line 355)
if app.data.(app.VN{1}) == app.data.(app.VN{i})
Error in SS14/import2 (line 288)
app.DateiAuswahl2;
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Button PrivateButtonPushedFcn.
Janik Ruge
Janik Ruge 2021-8-21
编辑:Janik Ruge 2021-8-21
I found my mistake. Later during the if queue i rename the field with renameStructField by mistake. Before, i had that command after the for queue and when restructuring some code i misplaced it. I made the mistake of not searching the failure later during the "for" queue.
This caused the program to rename the field, but not update VN, thus when continuing the "for" queue, the first field of "data" was renamed, while VN wasn't. I now update VN after renaming the Field during the for queue, which fixed the problem.
Thanks for the help anyways! Would not have found it without that precise help.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by