Create function to convert data type as table: 2 errors

조회 수: 1 (최근 30일)
Deborah Johnson
Deborah Johnson 2023년 2월 22일
편집: Walter Roberson 2023년 3월 2일
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));

채택된 답변

Voss
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
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.
Deborah Johnson
Deborah Johnson 2023년 3월 2일
편집: Walter Roberson 2023년 3월 2일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by