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

1 回表示 (過去 30 日間)
Leeee
Leeee 2019 年 4 月 11 日
コメント済み: Leeee 2019 年 4 月 17 日
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 件のコメント
Leeee
Leeee 2019 年 4 月 11 日
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 2019 年 4 月 11 日
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.

サインインしてコメントする。

採用された回答

Jan
Jan 2019 年 4 月 12 日
編集済み: Jan 2019 年 4 月 13 日
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 件のコメント
Jan
Jan 2019 年 4 月 16 日
編集済み: Jan 2019 年 4 月 16 日
"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 2019 年 4 月 17 日
@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.

サインインしてコメントする。

その他の回答 (3 件)

Leeee
Leeee 2019 年 4 月 12 日
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 件のコメント
Jan
Jan 2019 年 4 月 12 日
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 2019 年 4 月 12 日
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.

サインインしてコメントする。


Leeee
Leeee 2019 年 4 月 12 日
編集済み: Leeee 2019 年 4 月 12 日
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
  1 件のコメント
Jan
Jan 2019 年 4 月 13 日
編集済み: Jan 2019 年 4 月 13 日
The Google drive is a tedious external service. Use the file attached to my comment instead.

サインインしてコメントする。


Leeee
Leeee 2019 年 4 月 16 日
@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.

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by