フィルターのクリア

No output from a for and elseif loop

2 ビュー (過去 30 日間)
Mohammad R. Ghassemi
Mohammad R. Ghassemi 2021 年 10 月 15 日
コメント済み: Mohammad R. Ghassemi 2021 年 10 月 15 日
Hi,
I am trying to get the J matrix from the following simple code, however I receive no output for J:
str=readline('1C1S.txt','all');
P=strfind(str,'w');
for x=1:numel(str)
if isempty(P(x,1))
J=1;
elseif isequal(P(x,1),[9])
J=2;
elseif isequal(P(x,1),[4])
J=3;
elseif isequal(P(x,1),[4 9])
J=4;
end
end
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 10 月 15 日
What is readline() ? The readline() functions I can find in MATLAB require serial port or tcp port, and do not have a 'all' parameter.
Mohammad R. Ghassemi
Mohammad R. Ghassemi 2021 年 10 月 15 日
It is a useful function I have downloaded from the MathWorks.
It reads all strings from a text file (line by line) as they are (including spaces etc.).

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

採用された回答

DGM
DGM 2021 年 10 月 15 日
編集済み: DGM 2021 年 10 月 15 日
That should be readlines().
P is a cell array of numeric vectors, so address it accordingly.
Any assignment to J overwrites the prior value in J.
The case where none of the conditional tests is true is not handled.
The equality tests are probably wrong, but since there's no description of the intended logic, I don't know what to tell you.
To summarize:
isequal(P(x,1),[4 9])
is never true, because P(x,1) is a cell scalar, which is never equal to any numeric vector.
isequal(P{x,1},[4 9])
is only true if the vector in P{x,1} is identical to [4 9]. That is, it needs to be the same length and all elements must match.
  2 件のコメント
Mohammad R. Ghassemi
Mohammad R. Ghassemi 2021 年 10 月 15 日
Thank you for your comments. The function is in fact "readline", which I have downloaded from the MathWorks. It reads all strings from a text file (line by line) as they are (including spaces etc.).
I have replaced the parentheses with braces as you suggested. Now I have the J in the output, however it is a single number which I think belongs to the last (x) index.
There should be somthing wrong with my elseif command.
Mohammad R. Ghassemi
Mohammad R. Ghassemi 2021 年 10 月 15 日
Thanks a lot. I indexed J as J(x,1) and now I have the required complete output.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by