Invalid array indexing before arguing variable

For example,
sheetnames(FullFilePath)
This will give me string array (e.g., 5x1).
So, I tried to access directly one of elements like below
sheetnames(FullFilePath)(1)
But, this did not work.
A = sheetnames(FullFilePath)
A(1)
However, after arguing variable, if I do the same approach, then I could access the element.
Why does this happen? what is the difference between first and second approach.
I thought both looks the same.

댓글 수: 3

sheetnames is defined as as a function returning a string array.
You cannot directly use () or {} indexing on the return value from a function, so
sheetnames(FullFilePath)(1)
is invalid as that is trying to do () indexing on the return value of a function.
The only kind of indexing that you can do on the return value from a function is dot indexing
This is a limitation on the MATLAB parser .
There is some history behind it. Consider for example,
ones(2)
If you were permitted to directly index the result of a function call, you would not be able to tell whether ones(2) is a call to function ones with parameter 2, or if it is instead a call to function ones with no parameters, with the result to be indexed at (2) .
Coincidentally, @Walter Roberson, I just happened to be looking at your answer to the What is Missing from MATLAB? thread today. (That page takes forever to load now.)
Hokon
Hokon 2023년 5월 2일
@Walter Roberson I see. Thanks for your reply. Now it's clear to me. I'm curious about this.

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

 채택된 답변

Hokon
Hokon 2023년 5월 2일

0 개 추천

Above comment gave me answers.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2023a

태그

질문:

2023년 5월 1일

답변:

2023년 5월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by