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

1 回表示 (過去 30 日間)
William Cai
William Cai 2018 年 4 月 10 日
編集済み: Walter Roberson 2018 年 4 月 11 日
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 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by