Contains in cells extracted from a structure runs into char class problems

40 ビュー (過去 30 日間)
Herberto
Herberto 2025 年 1 月 21 日 15:03
編集済み: 埃博拉酱 2025 年 1 月 22 日 11:32
Hi everyone,
Gonna try to be brief. Basically I have a structure with a certain number of columns, and I need to know if a certain element already exists in the column, as to retrieve its index and then check other columns on the same row.
So if indications is the struct, and curr_ID is what I am trying to look for I coded something like this:
if secondPass && contains({indications.Patient_ID}, curr_ID)
idx = find(contains({indications.Patient_ID}, curr_ID),1, 'first');
end
(...)
Now the problem is that contains is outputing and error saying that "First argument must be text". Has anybody encountered this?
To be clear, contains works with cells, and if you do A = {indications.Patient_ID} you will see that A is a cell populated with the text from all the columns of Patient_ID. I also use contains with a different cell (ZZ for example) that originates from a function and there it works fine. I don't know if anybody has noticed but when using A={struct.field} what I actually get when looking using the variable viewer is a something that looks like this:
'ID111'
'ID112'
'ID113'
...
However, for my other cells of chars when looking using the varaible viewer I do not see the ' around the words. I wonder if this is a problem even though both class(A{1,1}) and class(ZZ{1,1}) both come out as char.
Thanks in advance for the help!

採用された回答

Walter Roberson
Walter Roberson 2025 年 1 月 21 日 17:23
移動済み: Walter Roberson 2025 年 1 月 21 日 19:58
abc = {'def', 'gaha', 'hello'}
abc = 1x3 cell array
{'def'} {'gaha'} {'hello'}
contains(abc, 'gaha')
ans = 1x3 logical array
0 1 0
so contains() works properly for cell arrays of character vectors.
This suggests that indications.Patient_ID are not character vectors. What shows up for
unique(cellfun(@class, {indications.Patient_ID}, 'uniform', 0))
?
I speculate that some of the Patient_ID might be empty [] instead of empty '' so the {} does not generate a cell array of character vectors.
  1 件のコメント
Herberto
Herberto 2025 年 1 月 21 日 17:57
移動済み: Walter Roberson 2025 年 1 月 21 日 19:59
You are absolutely right! There was one instance of [] which should not have happened but it did. This was then considered a double, and indeed unique(cellfun(@class, {indications.Patient_ID}, 'uniform', 0)) threw out char and double!
If I remove the empty eveything works fine.
Thank you very much for the help :)

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

その他の回答 (1 件)

埃博拉酱
埃博拉酱 2025 年 1 月 21 日 15:16
編集済み: 埃博拉酱 2025 年 1 月 22 日 9:59
I suggest you upload some .mat sample data. There are many ambiguities in your description.
The weirdest behavior in your code is to encapsulate a field of the struct into a cell: I can't imagine the point of such extra move. It is recommended that you try:
contains(indications.Patient_ID, curr_ID)
20250122
Code above is based my understanding that you have a scalar struct "indications" with a column vector field "Patient_ID". If that's not the case - that you actually have a column vector of structs "indications" with each of them have a char row vector field "Patient_ID", then you have @Walter Roberson.
  7 件のコメント
Herberto
Herberto 2025 年 1 月 22 日 11:01
I would argue that from the moment I said "a structure with a certain number of columns (...) and then check other columns on the same row" it was clear it was a non-scalar structure, and everybody else understood it clearly. Given the solution it is should also be clear that it is non-scalar, as the problem I described would not occur with a scalar structure.
The issue has been fixed, so I don't see the need to continue a line of questioning that looks more personal than technical at this point.
Enjoy your day.
埃博拉酱
埃博拉酱 2025 年 1 月 22 日 11:25
編集済み: 埃博拉酱 2025 年 1 月 22 日 11:32
@Herberto I really have no idea how I can distinguish the following two possibilities:
indicationsScalar=struct;
indicationsScalar.Patient_ID={'Patient1';'Patient2';'Patient3'}
indicationsVector=struct(Patient_ID={'Patient1';'Patient2';'Patient3'})
indicationsScalar =
包含以下字段的 struct:
Patient_ID: {3×1 cell}
indicationsVector =
包含以下字段的 3×1 struct 数组:
Patient_ID
These two variables have very different type structures and both fit your description. What's more interesting is that if indications is a scalar as I understood:
>> contains({indicationsScalar.Patient_ID}, 'Patient1')
错误使用 contains
第一个参数 必须为文本。
MATLAB will just throw the same error as you encountered without the need of having an empty double array in one of your rows. That's why my first instinct is that your "indications" is a scalar.

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

カテゴリ

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

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by