string matrix navigation with character string

1 回表示 (過去 30 日間)
Touts Touts
Touts Touts 2022 年 6 月 17 日
コメント済み: Walter Roberson 2022 年 6 月 23 日
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

回答 (2 件)

Matt J
Matt J 2022 年 6 月 17 日
編集済み: Matt J 2022 年 6 月 17 日
DataSour = ["Do" "Ri" "Mi"; "AA" "VV" "Fa"; "SS" "BB" "Do"; "Do" "yy" "zz"];
ToAssign = "Do";
[ColPos,RowPos]=find(DataSour'==ToAssign)
ColPos = 3×1
1 3 1
RowPos = 3×1
1 3 4
  4 件のコメント
Ferial Assmani
Ferial Assmani 2022 年 6 月 17 日
@ 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'.
Matt J
Matt J 2022 年 6 月 19 日
編集済み: Matt J 2022 年 6 月 19 日
You cannot use my solution unless you have a recent Matlab version. If you have upgraded, then convert DataSour to a string array, like in your posted example.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2022 年 6 月 17 日
[RowPos, ColPos] = find(ismember(DataSour, ToAssign))
  8 件のコメント
Ferial Assmani
Ferial Assmani 2022 年 6 月 19 日
@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
Walter Roberson 2022 年 6 月 23 日
No, I mean
MYFile(cellfun(@(x) ~ischar(x), MYFile)) = {''};

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2010b

Community Treasure Hunt

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

Start Hunting!

Translated by