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!