Consider preallocating for speed

2 次查看(过去 30 天)
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by