Effacer les filtres
Effacer les filtres

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

78 vues (au cours des 30 derniers jours)
Janik Ruge
Janik Ruge le 20 Août 2021
Modifié(e) : Janik Ruge le 21 Août 2021
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 commentaires
Janik Ruge
Janik Ruge le 21 Août 2021
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 le 21 Août 2021
Modifié(e) : Janik Ruge le 21 Août 2021
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.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by