symbol ={'R0';'R1';'R2';'R3';'R4';'R5';'R6';'R7';'R8';'R9';'R10';'R11';'R12';'R13';'R14';'R15';'SCREEN';'KBD';'SP';'LCL';'ARG';'THIS';'THAT'};
Address={'0';'1';'2';'3';'4';'5';'6';'7';'8';'9';'10';'11';'12';'13';'14';'15';'16384';'24576';'0';'1';'2';'3';'4'};
symboltable=table(symbol,Address);
k6 = isfield(symboltable.symbol,trim_Ainstruction)
where 'trim_Ainstruction' is a char ..and we are assigning trim_Ainstruction as screen(in char data type)
while am trying to search 'screen' in table 'symboltable' using above code, its returns zero eventhough the specific string is in the table. can anyone help me?

回答 (1 件)

Cris LaPierre
Cris LaPierre 2022 年 1 月 13 日

1 投票

You are not using isfield correctly. This function is for searching structure array fieldnames, not character vectors.
Consider looking into strcmp or ismember. For example
symbol ={'R0';'R1';'R2';'R3';'R4';'R5';'R6';'R7';'R8';'R9';'R10';'R11';'R12';'R13';'R14';'R15';'SCREEN';'KBD';'SP';'LCL';'ARG';'THIS';'THAT'};
Address={'0';'1';'2';'3';'4';'5';'6';'7';'8';'9';'10';'11';'12';'13';'14';'15';'16384';'24576';'0';'1';'2';'3';'4'};
symboltable=table(symbol,Address);
ismember('R6',symboltable.symbol)
ans = logical
1

5 件のコメント

Stephen23
Stephen23 2022 年 1 月 13 日
編集済み: Stephen23 2022 年 1 月 13 日
It is very odd that ISFIELD does not throw an error for data types that do not have fields:
isfield({},'a')
ans = logical
0
isfield([],'a')
ans = logical
0
I would not have expected that... a descriptive error message would be more useful, IMHO.
Cris LaPierre
Cris LaPierre 2022 年 1 月 13 日
VISHNU DIVAKARAN PILLAI
VISHNU DIVAKARAN PILLAI 2022 年 1 月 13 日
so can we use that result for if condition, i think it wil show an error.i need to use the result for if condition.
VISHNU DIVAKARAN PILLAI
VISHNU DIVAKARAN PILLAI 2022 年 1 月 13 日
ismember is working but its showing as array.. i need to use the result for if condition.. can u pls suggst?
Cris LaPierre
Cris LaPierre 2022 年 1 月 13 日
編集済み: Cris LaPierre 2022 年 1 月 13 日
Yes, logical results can be used for your if condition. That's new information that was not in the original question. What is the condition you want to have?
symbol ={'R0';'R1';'R2';'R3';'R4';'R5';'R6';'R7';'R8';'R9';'R10';'R11';'R12';'R13';'R14';'R15';'SCREEN';'KBD';'SP';'LCL';'ARG';'THIS';'THAT'};
Address={'0';'1';'2';'3';'4';'5';'6';'7';'8';'9';'10';'11';'12';'13';'14';'15';'16384';'24576';'0';'1';'2';'3';'4'};
symboltable=table(symbol,Address);
isfound = ismember('R6',symboltable.symbol);
if isfound
disp('Value found in symbol')
end
Value found in symbol
As you can see if the example above, the code you have provided here returns a single value. Share the code you are running.

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

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

質問済み:

2022 年 1 月 13 日

編集済み:

2022 年 1 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by