How to clear array in between multiple plots in for loop?
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mads Svendsen
le 24 Oct 2017
Commenté : Mads Svendsen
le 25 Oct 2017
Hi everyone,
I'm having some trouble doing multiple plots in a for loop. The arrays I use for my plot, stores data from previous iteration, messing up the plots.
Based on a .csv file containing data from 20+ devices, I wan't to seperate the data unique to each device and plot it. I do this as follows.
fid = fopen('Regular_events_24_10.csv');
readData = textscan(fid, '%f %s %q %f %f %f %s %s %s %s %s %s %s %s %s %s', 'Headerlines', 1, 'Delimiter', ',');
device_name = ["device_name1", "device_name2"....]
for u = 1:length(device_name)
k = 1;
for n = 1:length(readData{:,2})
if strcmp(strcat('"',device_name(u),'"'),readData{1,2} (n,1))
%disp(n);
for i = 1:16
temp_array{1,i} (k,1) = (readData{1,i} (n,1));
end
k = k + 1;
%disp(k);
end
end
device_time = temp_array{1,3} (:,1);
state = temp_array{1,4} (:,1);
progress = temp_array{1,6} (:,1);
desk_setting = temp_array{1,7} (:,1);
user_presence = temp_array{1,8} (:,1);
desk_height = temp_array{1,9} (:,1);
IR_obj = temp_array{1,12} (:,1);
figure('rend','painters','pos',[150 60 1100 750])
subplot(2,1,1);
hold on
title(device_title(u));
plot(dateAndTimeSerial,desk_height, 'r');
plot(dateAndTimeSerial,progress, 'k');
plot(dateAndTimeSerial,desk_setting_int, 'b');
datetick('x','HHMM');
legend('desk height','progress','desk setting int', 'Location','west');
hold off
subplot(2,1,2);
hold on
plot(dateAndTimeSerial,IR_obj, 'r');
plot(dateAndTimeSerial,user_presence_int, 'k');
plot(dateAndTimeSerial,state, 'b');
datetick('x','HHMM');
legend('IR obj', 'user presence int', 'state', 'Location','west');
hold off
end
I have omitted some of the code, where I typecast some of the data and so on. Should be irrelevant for my question.
I have tried to clear and overwrite all arrays at the end of the for loop, but I doesn't help anything.
Hope you can help me out,
Best regards Mads
For reference, this is what a failed plot looks like.

2 commentaires
KL
le 24 Oct 2017
What's the structure of your csv file? It looks like you can do the whole process a lot simpler, data extraction, plotting and everything. Why don't you attach a sample csv file with similar structure?
Réponse acceptée
Guillaume
le 24 Oct 2017
The immediate problem is that indeed you fail to clear your temp_array at the beginning of each step of your u loop. Simply fixed with
for u = 1:length(device_name)
temp_array = {};
%...
would fix that. Saying that, your code is very inefficient. You do need to learn to vectorise your code rather than looping over everything. Your n loop can be easily replaced. The i loop as well with a bit more effort. As a bonus, it wouldn't require to clear the temp_array as it would recreate a brand new one each time:
for u = 1:numel(device_name)
isdevice = strcmp(['"', device_name(u), '"'], readData{2}); %no need to index you can compare the whole column at once
temp_array = cellfun(@(col) col(isdevice), readdata, 'UniformOutput', false);
%...
However, your life would be a lot easier if you used readtable to import your data. A table is a lot easier to manipulate that a cell array of columns.
readdata = readtable('Regular_events_24_10.csv');
%import all done however we can clean up some columns
readdata.device_time = datetime(readdata.device_time);
readdata.desk_setting = strcmp(readata.desk_setting, 'True');
readdata.desk_setting = strcmp(readata.desk_setting, 'False');
%note that the above can be done by readtable if you override the import options. See the doc
Looping over the devices is trivial
for device = unique(readdata.device_id)'
device_name = device{1};
filtered_table = readdata(strcmp(readdata.device_id, device_name), :);
figure('rend','painters','pos',[150 60 1100 750]);
subplot(2,1,1);
hold on
title(device_name);
plot(filtered_table.device_time, filtered_table.desk_height, 'r');
plot(filtered_table.device_time, filtered_table.progress, 'k');
plot(filtered_table.device_time, filtered_table.desk_setting, 'b');
datetick('x','HHMM');
legend('desk height','progress','desk setting', 'Location','west');
hold off
%...
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!