Skip to content

Instantly share code, notes, and snippets.

@kitswas
Last active June 24, 2022 13:59
Show Gist options
  • Select an option

  • Save kitswas/34befa9d22c08a2ee79e2614eac92e3c to your computer and use it in GitHub Desktop.

Select an option

Save kitswas/34befa9d22c08a2ee79e2614eac92e3c to your computer and use it in GitHub Desktop.
Reads numbers with comma as a decimal separator from CSV files.
% -- DATA = read_improper_csv(FILE, NROWS, NCOLUMNS, DELIM, SKIP_LINES)
function data=read_improper_csv(file, nrows, ncolumns, delim=";", header_lines=0)
if (nargin() < 3 || nargin() > 5)
print_usage ();
endif
data = textscan (fopen(file, 'r'), "%s", "Delimiter", delim, "HeaderLines", header_lines);
data = cell2mat(data);
data = str2double(strrep(data,',','.'));
data = reshape(data, ncolumns, nrows); % elements are accessed in column-major order
data = transpose(data);
endfunction
@kitswas
Copy link
Author

kitswas commented Jun 24, 2022

See the following question on StackOverflow for the motivation behind this script.

How to read CSV files with GNU Octave if the delimiter is ;?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment