Unrecognized Field name in struct error, even though same Fieldname clearly in struct
Afficher commentaires plus anciens
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
Cris LaPierre
le 20 Août 2021
Modifié(e) : Cris LaPierre
le 20 Août 2021
How do you convert app.data_in (a table) to app.data (a structure)? And why do you convert it to a structure rather than leaving it as a table?
Janik Ruge
le 20 Août 2021
Cris LaPierre
le 20 Août 2021
Modifié(e) : Cris LaPierre
le 20 Août 2021
Next question - how are you loading your table into app.data_in? I'm curious how you are creating numbers where comma is the decimal separator, but your code needs it to be periods. Or why your numbers are coming in as strings.
readtable(filename,"DecimalSeparator",",")
Note that a comma may be the default delimiter. If you numbers are coming in as 2 separate number, specifiy the delimiter.
Janik Ruge
le 20 Août 2021
Modifié(e) : Janik Ruge
le 20 Août 2021
If it's working, you don't need to change it. I'm searching for what may be causing your issue. Based on everything you've shared, I cannot duplicate the error message. Have you left something out?
x1_1_1RadVorneRechts = rand(401,1);
x1_1_1RadVorneRechts_2 = rand(401,1);
x2_1_1Sitzschiene = rand(401,1);
x2_1_1Sitzschiene_2 = rand(401,1);
x3_1_1Sitzkissen = rand(401,1);
app.data_in = table(x1_1_1RadVorneRechts,x1_1_1RadVorneRechts_2,x2_1_1Sitzschiene,x2_1_1Sitzschiene_2,x3_1_1Sitzkissen)
app.VN = app.data_in.Properties.VariableNames
for i = 1 : length(app.VN)
% app.data.(app.VN{i}) = str2double(strrep(app.data_in.(app.VN{i})(1:end),',','.'));
app.data.(app.VN{i}) = app.data_in.(app.VN{i});
end
for i=2:width(app.data_in)
if app.data.(app.VN{1}) == app.data.(app.VN{i})
% Since I use random numbers, this will never be true
end
end
app.data
Can you share the full text of the error message (all the red text).
Just a note about your comparison in your if statement. It will only be true if every elementwise comparison is true (vector on the left is exactly the same as the vector on the right). Is that what you wanted? You can make that more explicit by using all:
if all(app.data.(app.VN{1}) == app.data.(app.VN{i}))
Janik Ruge
le 21 Août 2021
Modifié(e) : Janik Ruge
le 21 Août 2021
Walter Roberson
le 21 Août 2021
As a debugging step, just below the Dazu Feld 1 mit anderen comment, please add the following tests:
datafields = fieldnames(app.data)
missing = find(~ismember(app.VN, datafields));
if ~isempty(missing)
fprintf('Some VN fields do not match. Existing fields are\n');
existfields = strjoin(compose("|%s|", datafields), ', ');
fprintf('%s\n', existfields);
fprintf('VN data fields not found in app.data are:\n');
missfields = strjoin(compose("|%s|", app.VN{missing}), ', ');
fprintf('%s\n', missfields);
miss1 = app.VN{missing(1)};
fprintf('character codes for |%s| are: %s\n', miss1, cell2mat(double(miss1)));
else
fprintf('All VN data fields found\n');
end
Displaying the character codes for the first missing one might possibly help to see some subtle difference such as a hidden character like newline.
Janik Ruge
le 21 Août 2021
Janik Ruge
le 21 Août 2021
Modifié(e) : Janik Ruge
le 21 Août 2021
Réponses (0)
Catégories
En savoir plus sur Structures dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!