Resolving array indices must be positive integers or logical values error?

조회 수: 3 (최근 30일)
Emily
Emily 2024년 8월 23일
편집: Shivam Gothi 2024년 8월 23일
I am running this piece of code:
fold_name=pwd; a=regexp(fold_name,'/'); fold_name=fold_name(a(end)+1:end);
my pwd is the following: 'C:\testsamples'
when running the first two pieces of code, I do not get any error. When running the third piece I get this error:
"Array indices must be positive integers or logical values."
Any ideas as to why?
  댓글 수: 1
Stephen23
Stephen23 2024년 8월 23일
"Any ideas as to why?"
Beacuse the character / does not exist anywhere in your text, so a is empty:
fold_name = 'C:\testsamples';
a = regexp(fold_name,'/')
a = []

댓글을 달려면 로그인하십시오.

답변 (2개)

Star Strider
Star Strider 2024년 8월 23일
If you just want the string data after the slant, some optionos are —
pwd = 'C:\testsamples'
pwd = 'C:\testsamples'
LastString = extractAfter(pwd, '\')
LastString = 'testsamples'
BothStrings = strsplit(pwd, '\')
BothStrings = 1x2 cell array
{'C:'} {'testsamples'}
Another option is the fileparts function.
.

Shivam Gothi
Shivam Gothi 2024년 8월 23일
편집: Shivam Gothi 2024년 8월 23일
Hello,
From the above code, it seems that you are trying to access the name “testsamples” from the fold_name = 'C:\testsamples'.
The function “regexp(str,expression)” returns the starting index of each substring of str that matches the character patterns specified by the regular expression.
In the code, '/' was used instead of '\' in the 'expression' field of the function mentioned above. This seems to result in 'a' being an empty array, which is then used to index into “fold_name”. I believe this might be causing the error. I have attached a screenshot of the updated code along with the result obtained. Could you please confirm if this is the desired outcome?
Additionally, please refer to the following link to learn more about the regexp() function."
https://in.mathworks.com/help/matlab/ref/regexp.html
I hope this solves the issue.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by