Contains in cells extracted from a structure runs into char class problems
40 ビュー (過去 30 日間)
古いコメントを表示
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!
0 件のコメント
採用された回答
Walter Roberson
2025 年 1 月 21 日 17:23
移動済み: Walter Roberson
2025 年 1 月 21 日 19:58
abc = {'def', 'gaha', 'hello'}
contains(abc, 'gaha')
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 件)
埃博拉酱
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 件のコメント
埃博拉酱
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 Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!