Create function to convert data type as table: 2 errors

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));

 Accepted Answer

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 Comments

Hello Voss,
The program is still not running. Should I convert the array to table before passing it into the function?
Voss, I also just selected 'Run' function, I am told it needs more inputs? Thank you for the information about the array. I orginally pulled the data from a table and ran it code in the *,mls code and it works great. But I am asked now to create a 'function' to handle the same process. When I run the new function, it does not output the same error until I run the 'd = data_conversion(h). Is there another method that would work?
Debbie
"Should I convert the array to table before passing it into the function?"
The comments in the function in your question suggest that h inside the function is a table, so I was going by that assumption.
"I also just selected 'Run' function, I am told it needs more inputs?"
The function data_conversion (both yours and mine) takes one input, so you have to supply it one input, which is done by running a command like:
d = data_conversion(d)
It won't run with zero inputs, which is what clicking the Run button does.
Here's a more complete code, starting with reading the xlsx file you attached:
d = readtable('Book1.xlsx') % note that this has 'Vertical_Steering' not 'Vertical_Steering_Command', so I modified the function below
d = 1045×1 table
Vertical_Steering _________________ {'Altitude'} {'Altitude'} {'Altitude'} {'Altitude'} {'Altitude'} {'Altitude'} {'Altitude'} {'Altitude'} {'Altitude'} {'Altitude'} {'Altitude'} {'Altitude'} {'Altitude'} {'Altitude'} {'Altitude'} {'Altitude'}
d = data_conversion(d) % the new column is added
d = 1045×2 table
Vertical_Steering Vertical_Steering_Cmd_num _________________ _________________________ {'Altitude'} 0 {'Altitude'} 0 {'Altitude'} 0 {'Altitude'} 0 {'Altitude'} 0 {'Altitude'} 0 {'Altitude'} 0 {'Altitude'} 0 {'Altitude'} 0 {'Altitude'} 0 {'Altitude'} 0 {'Altitude'} 0 {'Altitude'} 0 {'Altitude'} 0 {'Altitude'} 0 {'Altitude'} 0
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)); % error
data(ismember(h.Vertical_Steering,'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
Hello Voss, I apologize for not getting back with you sooner but have been busy. Thank you about letting me know about the 'zeros'. The conversion is working now. Debbie
Debbie, I'm glad it's working!
Voss, can I call up the readtable with Matlab instead of xlsx? h=readtable(d.Vertical_Steering) If not, what do you recommend?
readtable is for reading a file (e.g., a spreadsheet file such as an .xlsx file) and returning the contents as a table variable.
I'm not sure what you have in mind. Can you explain?
Hello Voss, I have a cvs table with 20 columns of data. Only 2 columns have categorical data that require conversion to double data type with each as a standalone function. Each categorigal data column can have up to 12 data conversions ranging from 0 to 12. Due to the zeros call,
I'm looking for an alternate method. I could call the the original cvs file but it is really large. What do you suggest?
Do you get an "Out Of Memory" error when using zeros?
No. The orgiinal variable names are quite long when the original *.cvs file is read as readtable. I am told to set Variable Nmes to preserve.
I have experimented today trying different methods. Such as renaming that one particular variable name within the function but that didn't work. Then tried reading the MATLAB table as ' h = table(h.Vertical_Steering_Cmd), h = table(h,"Vertical_Steering_Cmd).
The function and matlab code is below with new attached file. Any suggestions?
function h = data_conversion(h)
% convert Vertical Steering Command categorical data to double data type as Table 'h'
% note: Altitude is set to '0'
h = readtable(h, 'Vertical_Steering_Cmd');
data = zeros(size(h.Vertical_Steering_Cmd));
data(ismember(h.Vertical_Steering_Cmd,'Limbo')) = 6;
% copy number data from table 'h' and
% add new column for Vertical Steering number data to table 'h'
h.Vertical_Steering_Cmd_num = data;
end
---------------------------
MATLABf
f = readtable("two_var_columns.csv");
% rename all variable headers in sequential order
d = renamevars(d,["FLTSIMocAutofltseqLateral_Steering_Command",
"FLTSIMocAutofltseqVertical_Steering_Command"],...
["Lateral_Steering_Cmd", "Vertical_Steering_Cmd"]);
% delete 1st row of erowgenios data
d([1],:) = [];
h = d.Vertical_Steering_Cmd
h = data_conversion(h.Vertical_Steering_Cnd)
error using indexing, Attempt to reference field of a non-structure array
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.
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!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!