Convert CSV / Array Cell to Dataset
Afficher commentaires plus anciens
Hi,
The goal is to put the attached into dataset: a) Directly from the file and b) From the cell array
For a) issue is there are commas inside one of the fields.
For b) I tried the below but this creates dataset of cell arrays instead of values directly.
I tried a step:
var{i}{j,1} = data2(j+1,i)
With:
data = dataset(var{1:end}, 'VarNames', varnames);
Full code:
function data = importfile1(workbookFile, sheetName, range)
% If no sheet is specified, read first sheet
if nargin == 1 || isempty(sheetName)
sheetName = 1;
end
% If no range is specified, read all data
if nargin <= 2 || isempty(range)
range = '';
end
%%Import the data
[~, ~, data2] = xlsread(workbookFile, sheetName, range);
data2(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),data2)) = {''};
% get headers
strings = char(data2{1,:})
varnames = {}
for i=1: length(strings(:,1))
varnames{i} = strtrim(strings(i,:))
end
var = {}
for i=1: length(data2(1,:))
for j=1: length(data2(2:end,2))
var{i}{j,1} = data2(j+1,i) % (!!!!!!) how to process this
end
end
% export into dataset
data = dataset(var{1:end}, 'VarNames', varnames);
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Text Files dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!