I'm trying to select out rows with strings from a spreadsheet that agree with certain conditions.
2 views (last 30 days)
Show older comments
Alexandra Brian
on 16 Mar 2017
Commented: User_in_Gim
on 21 Mar 2017
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
0 Comments
Accepted Answer
User_in_Gim
on 16 Mar 2017
So, on your two row, you want two have 2 variables, the first one fill with the title, and the second one with your data ?
You can do it with the following code :
[Data,Title] = xlsread(your_file,1,'A1:B28');
4 Comments
User_in_Gim
on 17 Mar 2017
Edited: User_in_Gim
on 17 Mar 2017
Just look at your workspace and you will see 2 mistakes. Your vectors B, C and F are 8087*1 while First_column_data is a 8088 *1 vector.
When you open this variable, we can see that First_column_datat first row is the title and the others rows are datas. This is why, I wrote :
First_column_data = First_column(2:28); % Get the data from the first column
Now on your code you can use this :
First_column_data = First_column(2:length(First_column)); % Get the data from the first column
Moreover, be careful with matrices D and E which are empty.
You don't need to write xlsread('conspreadsheet.xlsx', 1, 'H:H'); for each column, if you just want to read numbers.
See following code :
clear all;
clc;
[~,First_column] = xlsread('test.xlsx',1,'A1:A8088');
Title(1)= First_column(1); % Get the first row
First_column_data = First_column(2:length(First_column)); % Get the data from the first column
[Data,Title2] = xlsread('test.xlsx', 1, 'B1:H8088'); % Extract all your number data and title
Data = [First_column_data,num2cell(Data)]; % Concatenate your data
Title = [Title(1),Title2]; % Concatenate your title
clear First_column Title2 First_column_data;
I hope it will help you.
More Answers (1)
Alexandra Brian
on 17 Mar 2017
8 Comments
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).
See Also
Categories
Find more on Spreadsheets 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!