Transforming output of a function to a struct
Show older comments
I'm trying to convert the output of my function into a struct. These are the errors that I'am getting: (input has been removed due to privacy reasons.
Error using table2array (line 37)
Unable to concatenate the specified table variables.
Error in FuncGruntinginTennis (line 60)
GruntingDataHersteld = table2array(GruntingDataHersteld);
Error in HoofdscriptgruntinginTennis (line 13)
Participant =
FuncGruntinginTennis();
Caused by:
Error using categorical/cat (line 123)
Unable to concatenate a double array and a categorical array.
This is my main script where I want to summon my data.
%% C & C
clear
close all
%% Function
Participant = FuncGruntinginTennis();
This is my dataset:

This is the function that I have got so far:
function Participant = FuncGruntinginTennis(workbookFile, sheetName, dataLines)
%% Input handling
% If no sheet is specified, read first sheet
if nargin == 1 || isempty(sheetName)
sheetName = 1;
end
% If row start and end points are not specified, define defaults
if nargin <= 2
dataLines = [2, 125];
end
%% Set up the Import Options and import the data
opts = spreadsheetImportOptions("NumVariables", 8);
% Specify sheet and range
opts.Sheet = sheetName;
opts.DataRange = "A" + dataLines(1, 1) + ":H" + dataLines(1, 2);
% Specify column names and types
opts.VariableNames = ["Condition", "Subject", "Predx", "Predy", "Realx", "Realy", "Errory", "Errorx"];
opts.VariableTypes = ["categorical", "double", "double", "double", "double", "double", "double", "double"];
% Specify variable properties
opts = setvaropts(opts, "Condition", "EmptyFieldRule", "auto");
% Import the data
GruntingDataHersteld = readtable(workbookFile, opts, "UseExcel", false);
for idx = 2:size(dataLines, 1)
opts.DataRange = "A" + dataLines(idx, 1) + ":H" + dataLines(idx, 2);
tb = readtable(workbookFile, opts, "UseExcel", false);
GruntingDataHersteld = [GruntingDataHersteld; tb]; %#ok<AGROW>
end
%% Convert to output
GruntingDataHersteld = table2array(GruntingDataHersteld);
%% make a struct
speedvalue = 4;
TotalValues = max(size(GruntingDataHersteld))/speedvalue; %shows how many rows of values there are
counter = 1;
for i = 1 : Totalvalues
Participant(i).Subject = GruntingDataHersteld(i*speedvalue,2);
Participant(i).Condition = GruntingDataHersteld(1:speedvalue);
Participant(i).Pred.x = GruntingDataHersteld(counter,3);
Participant(i).Pred.y = GruntingDataHertsteld(counter,4);
counter = counter + 1;
end
end
If anyone has any ideas or tips, those would be much appreciated!
Thanks in advance!
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!