Two strcmp conditions in one if statement
2 views (last 30 days)
Show older comments
Emilee Cowan-Nelson
on 21 Mar 2022
Commented: Star Strider
on 21 Mar 2022
I am trying to write a code to organize my data into specific timepoints. I need to compare data from two cell arrays. If participant IDs AND dates from both arrays match, I want to put the data into the appropriate timepoint. I have written the code below, however the output is blank.
load('affected') %Data for only the affected arm of each participant in cell array
load('tally_full') %Tally sheet data in cell array
timepoint1 = [];
timepoint2 = [];
timepoint3 = [];
timepoint4 = [];
for length_affected = 2:length(affected) %First row is column headings so I'm starting at row 2
for length_tally = 3:length(tally_full) %First two rows are column headings so I'm starting at row 3
if strcmpi(affected(length_affected,1), tally_full(length_tally,1)) && strcmp(affected(length_affected,15), tally_full(length_tally,134))
timepoint1 = [timepoint1; affected(length_affected,:), tally_full(length_tally,134)];
elseif strcmpi(affected(length_affected,1), tally_full(length_tally,1)) && strcmp(affected(length_affected,15), tally_full(length_tally,135))
timepoint2 = [timepoint2; affected(length_affected,:), tally_full(length_tally,135)];
elseif strcmpi(affected(length_affected,1), tally_full(length_tally,1)) && strcmp(affected(length_affected,15), tally_full(length_tally,136))
timepoint3 = [timepoint3; affected(length_affected,:), tally_full(length_tally,136)];
elseif strcmpi(affected(length_affected,1), tally_full(length_tally,1)) && strcmp(affected(length_affected,15), tally_full(length_tally,137))
timepoint4 = [timepoint4; affected(length_affected,:), tally_full(length_tally,137)];
end
end
end
%Column 1 for both affected and tally_full are participant IDs
%Column 15 in affected is the date in numerical form, column 134 in tally_full is date in numerical form for timepoint 1, column 135 is
%timepoint 2, etc.
I have double checked manually and the IDs and dates are in the same format in both sheets, and the IDs and dates of each participant line up. I don't get any errors when I run the code, I simply get blank doubles for each of the timepoints.
0 Comments
Accepted Answer
Star Strider
on 21 Mar 2022
‘I don't get any errors when I run the code, I simply get blank doubles for each of the timepoints.’
That would indicate to me that the if conditions are not being met.
You need to take the strcmpi arguments apart to discover the reason. Choose appropriate values for ‘length_affected’ and ‘length_tally’ and check the results for each argument.
Then check the strcmpi results to see if the logical outputs match what you would expect from the arguments to them.
.
2 Comments
More Answers (0)
See Also
Categories
Find more on Dates and Time 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!