フィルターのクリア

How to make my code run faster

3 ビュー (過去 30 日間)
beginner94
beginner94 2018 年 4 月 24 日
コメント済み: beginner94 2018 年 5 月 12 日
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 件のコメント
Guillaume
Guillaume 2018 年 4 月 24 日
編集済み: Guillaume 2018 年 4 月 24 日

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
beginner94 2018 年 4 月 27 日
I needed the number of rows to know how many loops were necessary to get through the file and then I want to transmit the content to a matrix

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

採用された回答

Guillaume
Guillaume 2018 年 4 月 24 日
If all you want to do is count the number of lines in the file, then parsing it is a waste of time:
numlines = sum(fileread('modified.csv') == char(10))
is probably the fastest way to get the number of lines.
If you do want to actually access the content of the file, then readtable is probably the easiest way to get it. This does not involve transitioning through excel so should be much faster than xlsread. readtable can usually detect the file format on its own. If not you can give it hints.
  20 件のコメント
Walter Roberson
Walter Roberson 2018 年 5 月 7 日
ds = strcat(column{2}, {' '}, column{3})
and then you can use datenum() on the date string to decode into serial date numbers.
beginner94
beginner94 2018 年 5 月 11 日
編集済み: beginner94 2018 年 5 月 11 日
@Walter Roberson
This doesn't solve the problem unfortunately because I don't know yet how to specify the time/date format. For now I only get columns with "NaN"... Is there maybe a way to search for the colons in the time column with "strfind" and then return the hours, minutes and seconds seperately and put them together afterwards?

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

その他の回答 (1 件)

Walter Roberson
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.
  1 件のコメント
beginner94
beginner94 2018 年 5 月 12 日
Thank you!! This doesn't exactly work, probably because of my ancient Matlab version but I managed to adapt it and now it works fine. Thank you

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by