For loop over writes output with strcmp

Hello,
I have the following code. The probles is that for every for loop, there are mulitple occurences of firsy in second. So, after every run, L is over written. I would like the indexesfor of anything that matches to be added below the previous output of L, not overwritten. I tried a lot of things but couldn't figure this out. Please help.
for a=1:r
first=test1(a,1);
second=reactionType(:,3);
L=find(strcmp(second,first));
end

2 个评论

Would be real nice if you told what
test1
reationType
is...
they are both cell arrays of varying columns and rows containing mixed data (numbers and strings). test1 is basically my input array that where the 1st column needs to be cross-checked against the 3rd column of reactionType array for any matches. Then I need the index of those matches.

请先登录,再进行评论。

 采纳的回答

L=cell(1,numel(r)); % before loop (preallocation)
L{a}.... inside loop
^^^---just add this rest the same
celldisp(L) % after loop (to see the contents of L)

9 个评论

Okay that worked. However, now L is a cell array (1,r) where:
If there is data, the cell is a 11x1 double (for example) and if there is no data, i see [ ] (meaning blank or no matches were found).
Is there a way to trim L to only save cells where there is data and ignore the rest?
idx=~cellfun('isempty',L);
L(idx) % this will avoid all the empty arrays
I am sorry for my dumb questions but I am just learning matlab as I work on this. So I don't quite understand where to add this line?
Don't be sorry , at the end of the code.
This worked perfectly. Thank you so much.The three things I am struggling with in matlab is learning how to use textscan, format (when using text scan) and cellfunc properly.
I am sure it will be easier once I get better at this. But thank you for helping.
Anytime :) , read the documentation of textscan() it has plenty of examples.
Thank you!
I actually have a follow up. When I store the index of all matches in L{a} - in column 2 of L{1,1} (for example),how can I also store the input value from test_1 that is being compared to the master library reactionType inside for loop?
index comes from master library and input from test_1.
just preallocate as shown in the answer and follow it
Hello,
The problem I am encountering here is how to have string at L{1,1}(1,2) while L{1,}(1,1) is a number?
I keep getting NaN.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 MATLAB 的更多信息

产品

版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by