Error when function changes cell array size
1 view (last 30 days)
Show older comments
I have the following function,
function args = updateCell(args, string, value)
% args = updateCell(args, string, value) Takes a cell array, args,
% presumably name / value pairs, and updates the value of the next cell
% after the cell containing the string with value. i.e. changes the
% value for that name/value pair. If pair isn't present, it is appended.
if isempty(find(strcmp(args,string),1))
args(length(args)+1) = string;
args(length(args)+1) = value;
else
ind = find(strcmp(args,string));
if length(ind) > 1
error(['multiple instances of ' string 'value in args'])
return
end
args(ind+1) = value;
end
All the function does is take a cell array, check if the array has a particular string. If it does, update the next cell with the value provided. If it does not have that string, it is supposed to append the cell array with the string and value pair in new, consecutive cells.
Changing the value of an existing string/value pair works with no problem. For example if
args{1} = 'string'; args{2} = 1;
updateCell(args,'string',2)
ans =
'string' [2]
However, if I try to append a new set of values I get this error,
updateCell(args,'string1',10)
The left hand side is initialized and has an empty range of indices.
However, the right hand side returned one or more results.
Error in updateCell (line 8)
args{ind+1} = value;
I know I can use varargout to group returned values into an array but that gives me a nested cell array structure that I don't want. If I embed the code directly in higher level functions it works just fine, but I'd like this as a separate function. I'm running Matlab R2015a (8.5.0.197613).
Is there anyway just to append cells to the array and return the array? Thanks!
3 Comments
Guillaume
on 4 Jun 2015
The code you've posted is not the code you're running. I know that because the posted code would give you a Conversion to cell from double is not possible with your first example due to the use of () instead of {} on line 16, and because line 8 (or 9) in the posted code is not the same as the line 8 shown in the error.
Please post the exact code you're running (with warts and all), otherwise we stand no change of helping you.
Accepted Answer
Guillaume
on 4 Jun 2015
If the error message is the one you posted and the file you're editing is also the one you posted, then matlab is not running that file.
What does
which updateCell
return? You must have another updateCell.m somewhere in your path.
2 Comments
More Answers (1)
Nobel Mondal
on 4 Jun 2015
You ought to be using curly brackets for indexing here.
function args = updateCell(args, string, value)
if isempty(find(strcmp(args,string),1))
args{end+1} = string;
args{end+1} = value;
else
ind = find(strcmp(args,string));
if length(ind) > 1
error(['multiple instances of ' string 'value in args'])
end
args{ind+1} = value;
end
This code seems to be working fine for me - wasn't able to reproduce the error you showed.
>> myVar = {};
>> myVar = updateCell(myVar, 'First', 1)
myVar =
'First' [1]
>> myVar = updateCell(myVar, 'Second', 2)
myVar =
'First' [1] 'Second' [2]
>> myVar = updateCell(myVar, 'First', 10)
myVar =
'First' [10] 'Second' [2]
3 Comments
Nobel Mondal
on 4 Jun 2015
Edited: Nobel Mondal
on 4 Jun 2015
As far as I know (and Walter pointed out before), this error would come if
>> ind = [];
>> myArray{ind+1} = myVal; % Errors here
Can you please put a breakpoint on that line and check the value? It would be good to debug step-by-step.
It is surprising how the code is even reaching that line. It should be taking the other branch of if-else block.
What I mean: in case you have an empty cell and you're trying to add something for the first time. This condition should be true
isempty(find(strcmp(args,string),1))
And, it should never reach the line
args{ind+1} = value;
See Also
Categories
Find more on Environment and Settings 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!