Is there difference between using "data" and "[data]" when importing excel data

Hi All,
New to Matlab community. I am trying to import an Excel spreadsheet into Matlab using xlsread command, and store it as a numerical matrix. I am debating using either Data = xslread(sample.xlsx) or [Data] = xlsread(smple.xlsx)
I tried both commands and both seem to work. But I'd like to know, if there is any subtle difference.

 채택된 답변

Walter Roberson
Walter Roberson 2018년 4월 10일
편집: Walter Roberson 2018년 4월 11일
There is no difference.
[location] = value
is exactly the same as
location = value
when the location designates a single destination. Skipping the [] is effectively an abbreviation.
However if there are two output destinations such as
[row, col] = find(magic(9) == 7)
then you cannot skip the [] -- it is not valid to write, for example,
row, col = find(magic(9) == 7)
Or rather such a thing would mean to display row and then to assign the output of the find() to col.
Things can get slightly tricky if the location secretly designates a field of a non-scalar structure:
location.field = value
is valid if location is a scalar structure but not if location is a non-scalar structure. For non-scalar structures you need
[location.field] = value
Even though syntactically you cannot tell this apart from the case where location is a scalar structure, the [] are important. This is because if location is an existing non-scalar structure, then location.field triggers structure expansion into a comma separated list, as if you had written
location(1).field, location(2).field, ... location(end).field
and then under the rule that the [] is mandatory for multiple left-hand sides, you need the []
This can take some getting accustomed to.

추가 답변 (0개)

카테고리

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

태그

질문:

2018년 4월 10일

편집:

2018년 4월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by