Info
This question is closed. Reopen it to edit or answer.
How can I compile the values of a variable based on the value of a second variable?
1 view (last 30 days)
Show older comments
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!
Answers (1)
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!