string matrix navigation with character string
Show older comments
Hi; for my problem, I have a string matrix (DataSour) of size 3 in columns but strong in rows; for which I look to obtain a minimized matrix (DataFind) relative to a character string (ToAssign). Then I try to find the positions of the columns (ColPos) and rows (RowPos) where the character string is positioned.
DataSour = ["Do" "Ri" "Mi"; "AA" "VV" "Fa"; "SS" "BB" "Do"; "Do" "yy" "zz"]
ToAssign = "Do"
for results I have to find the minimized matrix
DataFind =
Do -- --
-- -- Do
Do -- --
and positions of columns
ColPos =
1
3
1
and positions of rows
RowPos =
1
3
4
Answers (2)
DataSour = ["Do" "Ri" "Mi"; "AA" "VV" "Fa"; "SS" "BB" "Do"; "Do" "yy" "zz"];
ToAssign = "Do";
[ColPos,RowPos]=find(DataSour'==ToAssign)
4 Comments
Touts Touts
on 17 Jun 2022
DataSour = ["Do" "Ri" "Mi"; "AA" "VV" "Fa"; "SS" "BB" "Do"; "Do" "yy" "zz"];
ToAssign = "Do";
DataFind=DataSour;
DataFind(DataFind~=ToAssign)="--";
DataFind(all(DataFind=="--",2),:)=[]
Ferial Assmani
on 17 Jun 2022
@ All ; It looks like my concern; I try to import my Excel matrix and retrieve the rows that contain the desired character string and otherwise retrieve the rows that do not contain the desired character string. Why it’s not working for me I have the R2016a version.
[~, ~, MYFile] = xlsread('MYFile.xlsx','Data','A2:D5');
MYFile(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),MYFile)) = {''};
DataSour = MYFile;
ToAssign = 'DO';
When I use
[RowPos, ColPos] = find(ismember(DataSour, ToAssign))
Error using cell/ismember (line 34)
Input A of class cell and input B of class char must be cell arrays of strings, unless one is a string.
When I use
[ColPos,RowPos]=find(DataSour'==ToAssign)
Undefined operator '==' for input arguments of type 'cell'.
Walter Roberson
on 17 Jun 2022
[RowPos, ColPos] = find(ismember(DataSour, ToAssign))
8 Comments
Ferial Assmani
on 17 Jun 2022
@ All ; It looks like my concern; I try to import my Excel matrix and retrieve the rows that contain the desired character string and otherwise retrieve the rows that do not contain the desired character string. Why it’s not working for me I have the R2016a version.
[~, ~, MYFile] = xlsread('MYFile.xlsx','Data','A2:D5');
MYFile(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),MYFile)) = {''};
DataSour = MYFile;
ToAssign = 'DO';
When I use
[RowPos, ColPos] = find(ismember(DataSour, ToAssign))
Error using cell/ismember (line 34)
Input A of class cell and input B of class char must be cell arrays of strings, unless one is a string.
When I use
[ColPos,RowPos]=find(DataSour'==ToAssign)
Undefined operator '==' for input arguments of type 'cell'.
Walter Roberson
on 18 Jun 2022
DataSour = ["Do" "Ri" "Mi"; "AA" "VV" "Fa"; "SS" "BB" "Do"; "Do" "yy" "zz"]
That code is not compatible with the R2010b release that you marked yourself as using. The release is important for this purpose, as some of the tools most suitable were introduced after r2010b.
Ferial Assmani
on 18 Jun 2022
@ Walter ; I have the R2016a version, but the error is in not in reading DataSour matrix ; is in find function, plz look :
[RowPos, ColPos] = find(ismember(DataSour, ToAssign))
or
[ColPos,RowPos]=find(DataSour'==ToAssign)
thnks
Walter Roberson
on 18 Jun 2022
The original posted code is incompatible with R2016a. string objects were introduced in r2016b and the use of double-quotes was introduced in r2017a.
Walter Roberson
on 18 Jun 2022
MYFile(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),MYFile)) = {''};
When you use the third output of xlsread then MATLAB examines every cell individually, and everything that looks like it might be a number gets converted to a number, and date-looking things might get converted to numeric too. If I recall correctly empty cells get converted into nan.
That cellfun you run converts nan to '' but does not convert any other numeric values. I would suggest to you two approaches:
1. Stop using xlsread. readtable() is more robust. In your older version using detectImportOptions can still be mostly advisable, possibly with some setvartypes() to be sure that you get the types right for columns that might be empty 2. If you must use xlsread() change the test in your cellfun to be just @(x)~ischar(x)
Ferial Assmani
on 19 Jun 2022
@Walter Roberson, plz do you mean ?
[~, ~, MYFile] = xlsread('MYFile.xlsx','Data','A2:D5');
MYFile(cellfun(@(x) ~ischar(x) && isnumeric(x) && isnan(x),MYFile)) = {''};
DataSour = MYFile;
ToAssign = 'DO';
[RowPos1, ColPos1] = find(ismember(DataSour, ToAssign))
[ColPos2,RowPos2]=find(DataSour'==ToAssign)
Ferial Assmani
on 19 Jun 2022
@Walter Roberson, plz, i got this error
Error using cell/ismember (line 34)
Input A of class cell and input B of class char must be cell arrays of strings, unless one is a string.
Walter Roberson
on 23 Jun 2022
No, I mean
MYFile(cellfun(@(x) ~ischar(x), MYFile)) = {''};
Categories
Find more on Cell Arrays 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!