Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Convert the following MATLAB function into a functionning script, where an array x is converted to a table. function t = array2table(x,varargin) if ~ismatrix(x) error(message('MATLAB:array2table:NDArray'));

Convert the following MATLAB function into a functionning script, where an array x is converted to a table.

function t = array2table(x,varargin)

if ~ismatrix(x)

error(message('MATLAB:array2table:NDArray')); end [nrows,nvars] = size(x);

pnames = {'VariableNames' 'RowNames'}; dflts = { {} {} }; [varnames,rownames,supplied] ... = matlab.internal.datatypes.parseArgs(pnames, dflts, varargin{:});

if supplied.VariableNames haveVarNames = true; else baseName = inputname(1); haveVarNames = ~(isempty(baseName) || (nvars == 0)); if haveVarNames if nvars == 1 varnames = {baseName}; else varnames = matlab.internal.datatypes.numberedNames(baseName,1:nvars); end else varnames = matlab.internal.tabular.private.varNamesDim.dfltLabels(1:nvars); end end

vars = mat2cell(x,nrows,ones(1,nvars));

if isempty(vars) % Create an empty table with the correct dimensions. t = table.empty(nrows,nvars); % This checks that number of supplied var names matches number of variables (zero) to % throw a consistent error. if haveVarNames, t.Properties.VariableNames = varnames; end if supplied.RowNames, t.Properties.RowNames = rownames; end else % Each column of x becomes a variable in t t = table.init(vars,nrows,rownames,nvars,varnames); end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions