Consider preallocating for speed

2 ビュー (過去 30 日間)
Hüseyin Uzun
Hüseyin Uzun 2021 年 6 月 21 日
コメント済み: Hüseyin Uzun 2021 年 6 月 21 日
Matlab tells me in line 11 to consider preallocating, but when I try to do it with line 7, matlab is still not happy with it. The value "Werte" is (1,16) in line 8 and in line 11 I add +1. I searched the forums but couldn't find a solution. What can i do?
1 while true
2 tic
3 data=importdata('C:\Users\Hüs\Documents\MATLAB\Datalogger\KlimaLoggProAuto.dat1');
4 alle=[data(8,1);data(11,1);data(21,1);data(24,1);data(34,1);data(37,1);data(47,1);data(50,1);data(60,1);data(63,1);data(73,1);data(76,1);data(86,1);data(89,1);data(99,1);data(102,1)];
5 string(alle);
6 a=split(alle,'"');
7 Werte=zeros(1,17); %preallocated value....might be unused.
8 Werte=a(:,2)';
9 Zeit=datetime('now');
10 Zeit=string(Zeit);
11 Werte=[Zeit Werte]; %Consider preallocating for speed.
12 Werte=cellstr(Werte);
13 [succes,message]=xlsappend('Datalogger.xlsx',Werte,1);
14 pause(600-toc)
15 end

採用された回答

Bjorn Gustavsson
Bjorn Gustavsson 2021 年 6 月 21 日
In your code you change the values and type of Werte 4 times. That seems a bit excessive. I'd try something like this:
1 while true
2 tic
3 data = importdata('C:\Users\Hüs\Documents\MATLAB\Datalogger\KlimaLoggProAuto.dat1');
4 alle = [data(8,1);data(11,1);data(21,1);data(24,1);data(34,1);data(37,1);data(47,1);data(50,1);data(60,1);data(63,1);data(73,1);data(76,1);data(86,1);data(89,1);data(99,1);data(102,1)];
5 string(alle);
6 a = split(alle,'"');
7 % Overwritten on the very next line: Werte=zeros(1,17); %preallocated value....might be unused.
8 Werte = a(:,2)';
9 Zeit = datetime('now');
10 Zeit = string(Zeit);
11 % Seems skippable: Werte=[Zeit Werte]; %Consider preallocating for speed.
12 ZeitWerte_string = cellstr([Zeit,Werte]);
13 [succes,message] = xlsappend('Datalogger.xlsx',ZeitWerte_string,1);
14 pause(600-toc)
15 end
HTH
  1 件のコメント
Hüseyin Uzun
Hüseyin Uzun 2021 年 6 月 21 日
Thanks a lot. It works =)

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by