Create function to convert data type as table: 2 errors
1 次查看(过去 30 天)
显示 更早的评论
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
------------------------------
Attempt to reference field of non-structure array.
h = zeros(size(h.Vertical_Steering_Command));
0 个评论
采纳的回答
Voss
2023-2-22
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 个评论
Walter Roberson
2023-2-28
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!