Storing data in an array: Need help modifying the code.

10 views (last 30 days)
I know this question has been asked a million times, but I although I searched through the answers I can't implement it on my code. I hope someone can clarify me how and what I've been doing wrong. Here is the code:
sensor_a=importdata('sensor.csv', ';');
sensor_b=importdata('sensor_2.csv', ';');
time_sensor_a=datenum(sensor_a.textdata);
time_sensor_b=datenum(sensor_b.textdata);
value_1 = sensor_a.data(:,1);
value_2 = sensor_a.data(:,2);
value_3 = sensor_a.data(:,3);
logical_state = sensor_b.data;
index=diff(logical_state);
index_a=(index==-1);
opening_times=time_sensor_b(index_a);
% Until here everything is working really well, it's just the creation of an index
end_time=opening_times(end,1);
counter_sensor=1;
counter_index=1;
values_final=ones(1879,1); %the final value should be an array with 1879 values, so I tried making this variable in order to store values in it but I don't know how exactly should it work
while end_time>time_sensor(counter_sensor,1);
if opening_times(counter_index,1)<time_sensor(counter_sensor,1)
data_index=counter_sensor-1;
values=sensor.data(data_index,:)
values_final(counter_index,:)=values_final(values); //it works without this line, but I get only the values from the last iteration
counter_index=counter_index+1;
end
counter_sensor=counter_sensor+1;
end
Thanks!
EDIT: Sorry, I thought writing less text will make it simpler. I will clarify as much as I can. So, I have 2 sensors which give me some readings, one is for temperature, humidity and co2 concentration, so it gives a data output with numerical values, while the other sensor is a logical sensor that is telling me whether the window is open or not. From the second sensor I made an index which points out the exact time when the window was opened for the first time - that part is working perfectly. Now with this loop I'm trying to check the value of sensor_a right before the window was opened. The logic of the loop seems to be good and the loop is running, my only problem is that I get just the last iteration in my output. I hope it's clear enough and thank you for your patience, I'm not very good at this!
EDIT 2: I tried by myself one more time and I solved it very simply, deleting the line that was giving me trouble and adding (counter_index,:) to my values variable, so the values are stored in each iteration.
  1 Comment
Stephen23
Stephen23 on 10 Jun 2016
@Nedim Hodzic: we could help you, but you have made it very hard for us! You don't tell us anything about what you are doing, what you want your code to do, nor does your code does not have any comments or help. How do you expect someone to "clarify me how and what I've been doing wrong" if you don't bother to tell us what the correct code behavior is ?
If you want help, then you need to edit your question and tell us what the code should be doing, give us some example data to test it on, and explain what the outputs should be.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements 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!