Matlab error: ??? The left hand side is initialized and has an empty range of indices.
1 view (last 30 days)
Show older comments
I'm trying to run the following code and get the error in the subject line above. It only occurs when I load the [] into handles.Filelist. Why does it show this error?
initial conditions are:
handles.Numfiles=1;
handles.Filelist=[];
FileName='Test1.mat';
[FileName, PathName]=uigetfile;
handles.Numfiles=handles.Numfiles+1;
%enter new entry into file list
handles.Filelist{handles.Numfiles,:}=[FileName]; %The error occurs here
1 Comment
Answers (2)
Walter Roberson
on 13 Jun 2013
Edited: Walter Roberson
on 13 Jun 2013
You start with an empty array, 0 x 0. You index that array at with an integer as the first index, and ":" as the second index. As the array is currently 0 x 0, the ":" referring to "all defined array indices" in the second dimension is going to take on the size of the current second dimension and so is going to take on size 0. So you are trying to initialize a 1 x 0 cell array with something that is not empty.
The meaning of A{B,:} is different when A is defined than when A is undefined. When A is undefined, A{B,:} would mean to initialize A with as many second dimension values as needed, but when A is defined then A{B,:} has to match the existing number of dimensions.
Interestingly, I found an inconsistency:
clear h
h{3,:} = 'hello'
h will then become a 1 x 1 cell containing 'hello'. Furthermore,
clear h
h{-5,:} = 'hello'
will be allowed !! with h becoming the same 1 x 1 cell.
But
clear h
h(3,:) = {'hello'}
will create a 3 x 1 cell with the first two empty and the third 'hello'
Meanwhile,
clear h
[h{3,:}] = 'hello'
The left hand side has h{:} inside brackets, which requires that h be defined, so that the number of expected results can be computed.
1 Comment
Walter Roberson
on 13 Jun 2013
Anyhow what you probably want is
handles.Filelist{handles.Numfiles} = FileName;
Kwen
on 13 Jun 2013
It looks like you should use a while loop instead of trying to implement this manually.
1 Comment
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!