Transforming output of a function to a struct

12 visualizaciones (últimos 30 días)
Rens Gietema
Rens Gietema el 16 de Mzo. de 2021
Comentada: Rens Gietema el 22 de Mzo. de 2021
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!

Respuesta aceptada

Aghamarsh Varanasi
Aghamarsh Varanasi el 19 de Mzo. de 2021
Hi,
The advantage with MATLAB table is that it can hold both 'non-categorical' and 'categorical' data. But when converting the table into an array, both 'categorical' and 'non-categorical' data cannot be concatenated. This is because the categorical array does not hold duplicates.
There are two workarounds for this issue.
  1. You can change the datatype for the first variable to be 'double' (Line 18), and your algorithm will work just fine.
  2. If having the datatype for the first variable as 'categorical' is important, you may not convert the table to an array, but can use convert it during the creation of the structure. For Example, comment out line 29, code for creating structure would be
speedvalue = 4;
TotalValues = max(size(GruntingDataHersteld))/speedvalue; %shows how many rows of values there are
counter = 1;
for i = 1 : TotalValues
Participant(i).Subject = table2array(GruntingDataHersteld(i*speedvalue,2));
Participant(i).Condition = table2array(GruntingDataHersteld(1:speedvalue,1));
Participant(i).Pred.x = table2array(GruntingDataHersteld(counter,3));
Participant(i).Pred.y = table2array(GruntingDataHersteld(counter,4));
counter = counter + 1;
end
Hope this helps

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by