Create function to convert data type as table: 2 errors

1 Ansicht (letzte 30 Tage)
Deborah Johnson
Deborah Johnson am 22 Feb. 2023
Bearbeitet: Walter Roberson am 2 Mär. 2023
Hello, I can run the code in a *.mlx file and its great but the code does not work as a function. Please help.
*.mlx file
h = d.Vertical_Steering_Command % selected a specific variable, categorial (Atltitude, Limbo)
*.m file (function call)
function Vertical_Steering_Cmd_num = data_conversion(h)
% convert Vertical Steering Command categorical data to double data type as Table 'h'
% note: Altitude is set to '0', Limbo is set to '6'
h = zeros(size(h.Vertical_Steering_Command)); % error
h(ismember(h.Vertical_Steering_Command,'Limbo'))=6;
% Output double data: add double data type from table 'h' and
% add new column for Vertical Steering Command number data to table 'h'
h.Vertical_Steering_Cmd_num = h
end
------------------------------
Error using indexing
Attempt to reference field of non-structure array.
h = zeros(size(h.Vertical_Steering_Command));

Akzeptierte Antwort

Voss
Voss am 22 Feb. 2023
One problem appears to be that the function expects a table but you're giving it a categorical array. I can't say for sure because I don't know how you're calling the function, but if you're using the h you define as
h = d.Vertical_Steering_Command % selected a specific variable, categorial (Atltitude, Limbo)
then that's a categorical array (if your comment is accurate).
Another problem is that you're using the variable h to mean two different things inside the function: the input table and the numeric data you want to add to the table.
Try defining the function as follows. I changed the numeric data variable to data (I'm not sure what the output should be so I output the new table h, with the added column).
function h = data_conversion(h)
% convert Vertical Steering Command categorical data to double data type as Table 'h'
% note: Altitude is set to '0', Limbo is set to '6'
data = zeros(size(h.Vertical_Steering_Command)); % error
data(ismember(h.Vertical_Steering_Command,'Limbo'))=6;
% Output double data: add double data type from table 'h' and
% add new column for Vertical Steering Command number data to table 'h'
h.Vertical_Steering_Cmd_num = data;
end
And when you call it, give it a table:
d = data_conversion(d); % d is your table; the function updates the table and returns it, and the new table is stored as d again
  12 Kommentare
Walter Roberson
Walter Roberson am 28 Feb. 2023
h = d.Vertical_Steering_Cmd
d is a table. d.Vertical_Steering_Cmd is the content of the variable Vertical_Steering_Cmd within the table.
h = data_conversion(h.Vertical_Steering_Cnd)
h is not a table or a struct or an object. You already extracted Vertical_Steering_Cmd into h so h.Vertical_Steering_Cnd would be like trying to use d.Vertical_Steering_Cmd.Vertical_Steering_Cnd -- not going to work.
Deborah Johnson
Deborah Johnson am 2 Mär. 2023
Bearbeitet: Walter Roberson am 2 Mär. 2023
Hello Voss and Walter, Thank you for assisting and your support. I apologize for my lack of experience. My co-worker and I figured out that I was trying to be too correct because I was specifying a table.variable_name which was actually an "array.variable_column" When I took off the variable_column, the function worked correctly and completes all of the data type conversions!
The function's solution:
----------------------
function i = data_conversion(h)
% convert Vertical Steering Command categorical data to double data type as Table 'h'
% note: Altitude is set to '0'
data = zeros(size(h));
data(ismember(h(:),'Limbo')) = 6;
i = data;
end
-----------------------
Thank you again!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by