Consider preallocating for speed
2 views (last 30 days)
Show older comments
Hüseyin Uzun
on 21 Jun 2021
Commented: Hüseyin Uzun
on 21 Jun 2021
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
0 Comments
Accepted Answer
Bjorn Gustavsson
on 21 Jun 2021
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
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!