I'm trying to select out rows with strings from a spreadsheet that agree with certain conditions.

2 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

User_in_Gim
User_in_Gim am 16 Mär. 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 Kommentare
Alexandra Brian
Alexandra Brian am 16 Mär. 2017
Hey, Thanks a lot that was really helpful. I wrote the following code from your example, but MATLAB returned an error following my code:
[~,First_column] = xlsread('conspreadsheet.xlsx',1,'A1:A8088');
Title(1)= First_column(1); % Get the first row
First_column_data = First_column(:); % Get the data from the first column
clear First_column;
A = xlsread('conspreadsheet.xlsx', 1, 'F:F'); %Choose row
B = xlsread('conspreadsheet.xlsx', 1, 'G:G'); %Choose row
C = xlsread('conspreadsheet.xlsx', 1, 'H:H'); %Choose row
D = xlsread('conspreadsheet.xlsx', 1, 'I:I'); %Choose row
E = xlsread('conspreadsheet.xlsx', 1, 'J:J'); %Choose row
F = xlsread('conspreadsheet.xlsx', 1, 'D:D');%Choose row
Data = [First_column_data,num2cell(A),num2cell(B), num2cell(C), num2cell(D), num2cell(E)]; % Matrix with both vectors
G = xlsread('conspreadsheet.xlsx', 4, 'E:E');%Choose row
*Error using horzcat Dimensions of matrices being concatenated are not consistent. *
User_in_Gim
User_in_Gim am 17 Mär. 2017
Bearbeitet: User_in_Gim am 17 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Alexandra Brian
Alexandra Brian am 17 Mär. 2017
Thank you Gim! Now I just need to figure out how to select column 1's value based on the other columns.
  8 Kommentare
Alexandra Brian
Alexandra Brian am 21 Mär. 2017
Bearbeitet: Alexandra Brian am 21 Mär. 2017
When I have the following code:
clear all;
clc;
[~,First_column] = xlsread('conspreadsheet.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('conspreadsheet.xlsx', 1, 'B1:H8088'); % Extract all your number data and title
Data = [First_column_data,num2cell(Data)]; % Concatenate the data
Title = [Title(1),Title2]; % Concatenate the title
clear First_column Title2 First_column_data;
A = Data(:,1); %choose column
B = Data(:,2); %Choose column
C = Data(:,3); %Choose column
D = Data(:,4); %Choose column
E = Data(:,5); %Choose column
F = Data(:,6); %Choose column
G = Data(:,7); %Choose column
ARule = A((B==1 & F==1) | (B==1 & F==1 & C==0) | (B==1 & F==0 & D==0 & E==0)); %select values whose rows fulfill the conditions listed
I receive the error: * Undefined operator '==' for input arguments of type 'cell'.
Error in concatenate (line 21) SymbolsRule = A((B==1 & F==1) | (B==1 & F==1 & C==0) | (B==1 & F==0 & D==0 & E==0)); %select values whose rows fulfill the conditions listed *
Are you adding this line of code to the concatenated code?
User_in_Gim
User_in_Gim am 21 Mär. 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).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by