How to clear array in between multiple plots in for loop?

조회 수: 11 (최근 30일)
Mads Svendsen
Mads Svendsen 2017년 10월 24일
댓글: Mads Svendsen 2017년 10월 25일
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
KL
KL 2017년 10월 24일
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
Mads Svendsen 2017년 10월 24일
편집: Mads Svendsen 2017년 10월 24일
Hey KL,
The csv file looks like this:
ID,"device_id","device_time","state","goal","progress","desk_setting","user_pressence","desk_height","BMP_pressure","BMP_temp","IR_obj","IR_amb","firmware_version","bluemix_id","bluemix_type"
212490,"Shifty_23","2017-10-24 10:50:10",3,3,0,False,True,"71.1","1017.334424","33.89","26.37","30.07",3,"Shifty_23","Shifty_bot"
212489,"Shifty_20","2017-10-24 10:48:58",8,3,0,False,False,"67.7","1023.169846","35.3","21.71","30.83",3,"Shifty_20","Shifty_bot"
212488,"Shifty_7","2017-10-24 10:48:56",8,3,0,False,False,"65.7","1023.018408","33.89","23.27","30.95",3,"Shifty_7","Shifty_bot"
212487,"Shifty_19","2017-10-24 10:48:52",8,3,0,False,False,"67.3","1022.899485","31.7","22.41","28.35",3,"Shifty_19","Shifty_bot"
212486,"Shifty_6","2017-10-24 10:48:50",8,3,0,False,False,"77.2","1023.477279","32.08","23.35","29.05",3,"Shifty_6","Shifty_bot"
212485,"Shifty_17","2017-10-24 10:48:47",8,3,0,False,False,"69","1022.936831","32.17","22.25","28.37",3,"Shifty_17","Shifty_bot"
I'm not using Matlab very often, so I won't be suprised if there's an easier and simpler way ;)
Best regards Mads

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2017년 10월 24일
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
%...
  댓글 수: 1
Mads Svendsen
Mads Svendsen 2017년 10월 25일
Thanks a lot Guillaume :)
It works perfectly and it's a lot simpler.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by