How can I find a corresponding data value to a text value?

I read an Excel file which contains text and numerical data. It looks like this:
For each trial I have to see whether there is a looking ('yes') and eventually add up the durations of all the yesses for each trial.
I found the correct trial with the following code:
T = readcell(BehDataParticipant2);
T_trial1 = strcmp(T, 'Trial 1');
It resulted in zeros and ones throughout the whole table. But now I don't know how to find a 'yes' and the corresponding duration in those corresponding rows. Can you help me? Thanks!

 Accepted Answer

tToy=readtable(BehDataParticipant2,'ReadVariableNames',1,'VariableNamingRule','preserve');
tToy.LookLeftToy=categorical(tToy.LookLeftToy);
tToySummary=groupsummary(tToy,tToy.LookLeftToy,'sum',tToy.("Duration - msec"))

6 Comments

The table data class and grouping variables are extremely powerful feature of MATLAB -- they're not the only hammer in the toolbox, but any more generally the one I gravitate towards frist until find the nail doesn't fit.
Thank you again for your help. It worked out great. Because of your help I got the following summary:
If I may ask you one more question, I want to calculate the percentage of looking in trial 1 and trial 2 from the total looking time (trial 1 + 2). The rows containing <undefined> can be deleted. I tried some code, but I get errors like 'Check for incorrect argument data type or missing argument in call to function 'find'', or the code runs but I get ans = 0x0 empty logical array.
LL_tr1 = find(strcmpi(TSummary_LookLeft(:,"Trial"),'Trial 1')) & find(strcmpi(TSummary_LookLeft(:,"LookLeftToy"),'Yes'));
So, to sum up: I want to find the duration time of the row containing 'Trial 1' and 'yes' and the duration time of the row containing 'Trial 2' and 'yes'. Thank you already!
Why not remove the undefined entries first? You didn't show your actual code so I'll revert to the previous example...
tToy=readtable(BehDataParticipant2,'ReadVariableNames',1,'VariableNamingRule','preserve');
tToy.LookLeftToy=categorical(tToy.LookLeftToy);
tToy.LookLeftToy(isundefined(tToy.LookLeftToy,:))=[];
tSummLeft=groupsummary(tToy,tToy.LookLeftToy,'sum',tToy.("Duration - msec"));
Or, of course, if you want/need to keep the observations in the original, the same logic works on the summary table as well.
Then, the summation and ratios are simple enough...
I'm sorry, was thinking too difficult. But I get it now, thanks!
Understand...is easy to do when still learning one's way around in MATLAB.
I've observed it is the penchant to also want to create new, external variables from the table containing the data to work with instead of using the table reference to the data in the table directly. As a general practice, try to avoid making unneccessary copies of data you already have; there's an extensive section in the documentation on tables on how to reference table data that is invaluable and mandatory reading to use tables effectively -- and for disparate data types and convenience in higher-level coding abstraction, the table is about the alpha dog in the pack of tools available in MATLAB.

Sign in to comment.

More Answers (1)

Create the masks only over the relevant columns. Then use a logical combination of the masks to select the entries from the first column.
T = {1180, 'Trial 1', 'yes';
1150, 'Trial 1', [];
860, 'Trial 1', 'yes';
1350, 'Trial 2', 'yes';
940, 'Trial 2', 'yes'};
maskt1 = strcmpi(T(:,2),'trial 1');
maskt2 = strcmpi(T(:,2),'trial 2');
maskyn = strcmpi(T(:,3),'yes');
maskedvalues1 = cell2mat(T(maskt1 & maskyn,1))
maskedvalues1 = 2×1
1180 860
maskedvalues2 = cell2mat(T(maskt2 & maskyn,1))
maskedvalues2 = 2×1
1350 940

Categories

Commented:

dpb
on 15 Feb 2022

Community Treasure Hunt

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

Start Hunting!