How to make my code run faster
古いコメントを表示
Good Morning,
this line apparently takes 21s to run:
s = size(xlsread(filename, 1, 'F:F'))
It is supposed to count the number of rows of an Excel sheet. It is actually a .csv data file, but the values are not separated by commas but by semicolons, so I cannot use the Matlab function ''csvread''. Is there maybe a faster or more elegant way to do this? And is it the ''xlsread'' oder the ''size'' that takes so long...?
Thank you in advance
2 件のコメント
xlsread needs to
- start excel
- get excel to read the file
- parse the text to convert it into numbers
- read the data from the worksheet
The first three operations in particular are going to take some time. Excel is not a lightweight program and reading data from disk always takes time.
All that just to get the number of rows in a text file?
beginner94
2018 年 4 月 27 日
採用された回答
その他の回答 (1 件)
Walter Roberson
2018 年 5 月 11 日
S = fileread('modified.csv');
S(S==',') = '.'; %file uses comma for decimal point
fmt = ['%f%s%s', repmat('%f', 1, 8)];
datacell = textscan(S, fmt, 'HeaderLines', 3, 'Delimiter', ';', 'MultipleDelim', true, 'CollectOutput', true);
ds = strcat(datacell{2}(:,1), {' '}, datacell{2}(:,2));
dates = datenum(ds, 'dd.mm.yyyy HH:MM:SS');
all_data = [datacell{1}, dates, datacell{3}];
Now all_data is an all-numeric array, 10 columns wide, in which the second column is the serial date number.
カテゴリ
ヘルプ センター および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!