MATLAB: Is there difference between using “data” and “[data]” when importing excel data

maker

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.

Best Answer

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.
Related Question