Hi Daniele
I have provided a function for you that seems to do the trick (attached and in code-block below).
Feel free to use it without any limitations - and please alter it as you wish (no rights reserved)
This may not be the most elegant solution but as it is based on textscan it should be fairly robust, customizable and reasonably fast.
Be aware that it is currently hard-coded that the delimiter is ',' as in your example (this can be changed in line 34 and 35 if desired - and maybe turned into an input if it changes often).
Error handling, testing and help is kept at a minimum.
function outputtable = csvfile2table(csvfile)
if nargin == 0 || isempty(csvfile)
[filename,filepath] = uigetfile({'*.csv' 'CSV file (*.csv)'},'Select .CSV file to read into table');
csvfile = fullfile(filepath,filename);
end
fid = fopen(csvfile);
if fid <= 0
err = MException([mfilename,':MissingFile'],'File %s is not existing or cannot be read',csvfile);
throw(err);
end
filecontent = textscan(fid,'%s','delimiter','\n');
fclose(fid);
tablecontent = cellfun(@(x)textscan(x,'%s','delimiter',','),filecontent{1}(2:end));
tableheaders = cellfun(@(x)textscan(x,'%s','delimiter',','),filecontent{1}(1));
outputtable = cell2table(strtrim(horzcat(tablecontent{:})'),'VariableNames',tableheaders{1});
Best Answer