How to upload 800 csv files that only contains numbers in a cell keeping their names

1 view (last 30 days)
Hello, I would like to import my 800 csv files by keeping their name so I can identify them afterwards and perform my image processing. I try this script but it takes too much time. Thank you any help is welcome.
fichiersRecherches = '*.csv';
[FileName,PathName] = uigetfile(fichiersRecherches,'Sélectionnez les fichiers qui ont pour extention csv', 'MultiSelect', 'on');
FileName = cellstr(FileName);
m = cell(1,length(FileName));
for i_file = 1:size(m,2)
m{i_file} = xlsread(fullfile(PathName, FileName{i_file}));
end
  6 Comments
Leeee
Leeee on 11 Apr 2019
I tried this without converting to xlsx and I have this error message:
pathname = uigetdir();
m = cell(2,400);
for i = 1:2
for j = 1:100
for k = 1:4
filename = sprintf('cycle%d_image%d_%d.csv', j, k, i);
if exist(fullfile(pathname, filename), 'file')~=2
error('File not found: %s', fullfile(pathname, filename))
end
m{i,4*(j-1)+k} = csvread(fullfile(pathname, filename));
end
end
end
/////////////The answer/////////
Error using dlmread (line 147)
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 3) ==>
;1167,000000;1147,000000;1137,000000;1182,000000;1131,000000;1105,000000;1230,000000;1174,000000;1045,000000;1115,000000;1164,000000;994,000000;1065,000000;942,000000;971,000000;1111,000000;1319,00000...
Error in csvread (line 48)
m=dlmread(filename, ',', r, c);
Error in bakh2 (line 15)
m{i,4*(j-1)+k} = csvread(fullfile(pathname, filename));
Jan
Jan on 11 Apr 2019
For "use the profiler":
doc profile
For the message
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 3) ==>
There seems to be an unexpected string in row 1 and column 3.

Sign in to comment.

Accepted Answer

Jan
Jan on 12 Apr 2019
Edited: Jan on 13 Apr 2019
As I said: The number contain commas as decimal separators. Before such a file can be imported, in much be converted. This costs a lot of time.
Maybe this is more efficient to fix the file contents:
function Comma2Dot(FileName)
file = memmapfile(FileName, 'writable', true);
comma = uint8(',');
point = uint8('.');
file.Data(transpose(file.Data == comma)) = point;
end
Afterwards a simple fscanf(fid, '%g;', [472, Inf]) will import the data efficiently.
By the way, all decimal places are "000000" only. This means that storing only the integer part would be ways better. Storing the data in binary format would even better again. So teh main problem is that a really inefficient file format has been chosen. and you cannot blame the import of MATLAB.
With the original data:
tic;
Comma2Dot('test2.csv');
% Emulatre DLMREAD:
fid = fopen('test2.csv');
C = textscan(fid, '', -1, 'Delimiter', ';', 'EndOfLine', '\r\n', ...
'CollectOutput', 1);
fclose(fid);
data = C{1};
toc
% Elapsed time is 0.120229 seconds.
Now try:
% Write data in binary format:
fid = fopen('TestData.bin', 'W');
% Number of dimensions and size
fwrite(fid, [ndims(data), size(data)], 'uint64');
fwrite(fid, data, 'uint16');
fclose(fid);
tic;
fid = fopen('TestData.bin', 'r');
nDimsData = fread(fid, 1, 'uint64');
sizeData = fread(fid, [1, nDimsData], 'uint64');
data = fread(fid, sizeData, 'uint16');
fclose(fid);
toc
% Elapsed time is 0.013736 seconds.
The timings might be unfair, because reading data, which have been written to disk directly before, will be taken from the disk cache. But the accelerateion is expected: Compare the file sizes of 2'632 kB for the text file and 442 kB for the binary file.
So the actual optimization is not to improve the Matlab code, but to use a smart format to store the file.
  8 Comments
Jan
Jan on 16 Apr 2019
Edited: Jan on 16 Apr 2019
"It does not work" is a lean explanation and does not allow to understand, what the problem is. Please post the details.
I've used the posted methods successfully and the timings show, that it will be much faster, if you use a proper file format instead of a text files with commas and meaingless zeros as decimal places. I've explained thois exhaustively already and do not know, how I can help you now.
Leeee
Leeee on 17 Apr 2019
@Jan Sorry it's working now your method I did not fit well to my script.
But if you want to make it easier you just have to use strrep instead of strrepInFile thanks to one of your suggestions on another topic.

Sign in to comment.

More Answers (3)

Leeee
Leeee on 12 Apr 2019
Thanks for your answers I followed your advice but it still takes 2 minutes code and would anyone have a better idea?
And the profile gives her:
aa.PNG
pathname = uigetdir();
m = cell(2,400);
for i = 1:2
for j = 1:100
for k = 1:4
filename = sprintf('Cycle%d_Image%d_%d.csv', j, k, i);
if exist(fullfile(pathname, filename), 'file')~=2
error('File not found: %s', fullfile(pathname, filename))
end
strrepInFile(fullfile(pathname, filename), ',', '.');
m{i,4*(j-1)+k} = dlmread(fullfile(pathname, ['new_' filename]), ';');
delete(fullfile(pathname, ['new_' filename]))
end
end
end
  3 Comments
Jan
Jan on 12 Apr 2019
How large are the files? I'm confused by "strrepInFile". Can you post some example data? Perhaps it contains commas as decimal separators?
Leeee
Leeee on 12 Apr 2019
Hello Rik,
No I admit that it is not very efficient my computer and I will try on a SSD SATA see what gives it.

Sign in to comment.


Leeee
Leeee on 12 Apr 2019
Edited: Leeee on 12 Apr 2019
Merci pour vos réponses et une partie attachée de mes données!
https://drive.google.com/file/d/1wKvOSL9tJUyEANB2tO2J3RXPQEc8-xQz/view?usp=sharing

Leeee
Leeee on 16 Apr 2019
@Jan I followed one of your methods using strrep in place of strrepInFile and it has greatly reduced the time and I only have the dlmread that takes up to 80% of the script time.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!