MATLAB Answers

How can I compile the values of a variable based on the value of a second variable?

1 view (last 30 days)
Katherine
Katherine on 26 Dec 2015
Answered: dpb on 28 Dec 2015
I have a lot of files like the one attached wherein I need to extract three variables and compile their values based on each other. I am able to extract these values to the work space through this code I was provided:
[~,~,raw] = xlsread('Baseline Day 4 112915.csv');
%filter lines that starts with "S:" and "Q:"
S_lines = raw(strncmp('S:', raw,2));
Q_lines = raw(strncmp('Q:', raw,2));
G_lines = raw(strncmp('G:', raw,2));
%get values and transform to numbers
tmp = cellfun(@(x) str2double(x(3:end)), S_lines,'UniformOutput', false);
S_vals = cell2mat(tmp);
tmp = cellfun(@(x) str2double(x(3:end)), Q_lines,'UniformOutput', false);
Q_vals = cell2mat(tmp);
tmp = cellfun(@(x) str2double(x(3:end)), G_lines,'UniformOutput', false);
G_vals = cell2mat(tmp);
I need the S_vals, which correspond to time, to be separated into two categories based on Q_vals. One in which Q is >= 1 and the other in which Q = 0, which I am able to do through this code:
Avoidance_1_Bin = (S_vals(Q_vals == 0&G_vals == 1));
Escape_1_Bin = (S_vals(Q_vals >=1&G_vals == 1));
It becomes more complicated however because I need the S_vals that directly proceed the S_vals with a Q >= 1 to be included in the Escape_1_Bin workspace sheet, but not in the avoidance_1_bin workspace sheet.These S_vals will always have a Q = 0. I've been trying to work with if/else statements, but without much success.
I'm a bit lost as I am new to matlab and coding in general. I would greatly appreciate any ideas you may have.
Thank you!

  2 Comments

dpb
dpb on 26 Dec 2015
It'd be much simpler if you you gave a small sample of the dataset to work with but a value of 0 and the subsequent >0 would be the locations given by the logical addressing vector
ix=(x==0) & [diff(x)>0 true];
Katherine
Katherine on 26 Dec 2015
I'm sorry, I'm really not familiar at all with matlab and the solution I'm looking for is probably quite complex. Attached are the two data output from my current code.
The first few lines in the avoidance_1_bin, for example, are:
0.0100000000000000
30.3400000000000
31.1500000000000
61.1500000000000
62.0700000000000
92.0900000000000
124.310000000000
While the first few from the escape_1_bin are:
0.350000000000000
94.2700000000000
126.150000000000
Counter to my initial thought of included the Q value previous to a Q value >=1, would it be simpler to have an output that looks like this? Wherein the avoidance_1_bin numbers (on the left) line up sequentially to the escape_1_bin numbers (on the right).
0.0100000000000000----------0.350000000000000
30.3400000000000
31.1500000000000
61.1500000000000
62.0700000000000
92.0900000000000 ----------94.2700000000000
124.310000000000 ---------- 126.150000000000
Thank you for your time and help, I sincerely appreciate it
-Katie

Sign in to comment.

Answers (1)

dpb
dpb on 28 Dec 2015
Using a and e for the two arrays, respectively,
>> ix=interp1(a,[1:length(a)].',e,'nearest','extrap')
ix =
1
6
7
>>

  0 Comments

Sign in to comment.