How to clear array in between multiple plots in for loop?
Show older comments
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 Comments
KL
on 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?
Mads Svendsen
on 24 Oct 2017
Edited: Mads Svendsen
on 24 Oct 2017
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB 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!