I'm trying to select out rows with strings from a spreadsheet that agree with certain conditions.
Show older comments
Hi,
I'm trying to write a code to select out the strings that have a 1 in the corresponding column. This is the code I wrote:
X = xlsread('stringspreadsheet.xlsx', 1, 'B:B'); %Choose row
strings= xlsread('stringspreadsheet.xlsx', 1, 'A:A'); %Choose row
stringsRule1= strings(X>0);
Any help would be greatly appreciated.
Best, A
Accepted Answer
More Answers (1)
Alexandra Brian
on 17 Mar 2017
0 votes
8 Comments
Alexandra Brian
on 17 Mar 2017
Edited: Alexandra Brian
on 19 Mar 2017
User_in_Gim
on 17 Mar 2017
Edited: User_in_Gim
on 17 Mar 2017
You're welcome !
I'm sorry but I ddon't understand what you want to do. Can you give me an example or provide more information ?
Alexandra Brian
on 19 Mar 2017
User_in_Gim
on 20 Mar 2017
Edited: User_in_Gim
on 20 Mar 2017
Hi,
For me, your code line should works :
Rule1 = A(B==1 & F==1 | B==1 & F==1 & C==0 | B==1 & C==0 & D==0 & E==0); %select values whose rows fulfill the conditions listed
Your conditions were not clear (D==1 AND H==1) OR (D==1 AND E==0 AND H==1) (the second condition does nothing more than the first one), so I made another example :
If I want to extract all data from column A which depending on following conditions (each column name are the ones on the excel sheet) :
D ==1 && H ==1
or D==1 && E==0 && H ==1
or H == 1 && E==0 && G ==0 && H ==0.
New_A = A((D==1 & H==1) | (F==0 & E==1)| (H==0 &G==0 & F==1));
And you get a 2150 *1 vector. D ==1 && H ==1 : 1000 hits.
D==1 && E==0 && H ==1 : 270 hits - (15 hits from the first line D=1 and H = 1)
H ==0 && G ==0 && F ==1 : 895 hits
So 1000 +255+895 = 2150.
Alexandra Brian
on 20 Mar 2017
User_in_Gim
on 20 Mar 2017
Edited: User_in_Gim
on 20 Mar 2017
The strings ?
With New_A = A(expression) you 'll get all data that agree the expression. I obtain a 2150*1 column vector with information from A
Alexandra Brian
on 21 Mar 2017
Edited: Alexandra Brian
on 21 Mar 2017
User_in_Gim
on 21 Mar 2017
Yes, because your data type is cell. You should make your logical indexing with array.
So converting your data with cell2mat will clear the error. But don't convert your first column (A).
A= Data(:,1); %choose column
D = cell2mat(Data(:,4)); %Choose column
E = cell2mat(Data(:,5)); %Choose column
F = cell2mat(Data(:,6)); %Choose column
G = cell2mat(Data(:,7)); %Choose column
H = cell2mat(Data(:,8)); %Choose column
mat= ((D==0)&(E==1)&(F==0)&(G==1)&(H==0));
Be carefull with your data type. You can change the code according to your needs. For example, you can skip the following line Data = [First_column_data,num2cell(Data)] and just keep the first Data array. Doing this, you don't need to convert with cell2mat anymore.
But I choose to wrote Data = [First_column_data,num2cell(Data)], beacause i think it's more convenient. ( --> one variable with all your titles and another one with all your data).
Categories
Find more on Logical 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!