Error when function changes cell array size

1 Ansicht (letzte 30 Tage)
Aaron
Aaron am 3 Jun. 2015
Kommentiert: Aaron am 4 Jun. 2015
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 Kommentare
Guillaume
Guillaume am 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.
Aaron
Aaron am 4 Jun. 2015
Bearbeitet: Aaron am 4 Jun. 2015
This is the exact code I used. I cut and pasted the code above into a new function to verify I got the same response above. For whatever reason when I run the code from the command window, Matlab makes the change from brackets to curly brackets in the error message. I assume this is because Matlab is interpreting my input to be with curly bracket, but that's just a guess. It would also be consistent with why I don't get the error message you suggest. In anycase, per the comment below I did try changing the code to use curly brackets and the output was the same (see below). Thanks for your input!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 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 Kommentare
Nobel Mondal
Nobel Mondal am 4 Jun. 2015
I was thinking the same.
Aaron
Aaron am 4 Jun. 2015
Bearbeitet: Aaron am 4 Jun. 2015
which updateCell
returns only one instance of the file and it's in the location I put it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Nobel Mondal
Nobel Mondal am 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 Kommentare
Nobel Mondal
Nobel Mondal am 4 Jun. 2015
Bearbeitet: Nobel Mondal am 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;
Aaron
Aaron am 4 Jun. 2015
Problem solved, Microsoft problems. The file was saved to a OneDrive folder and failing to update it thus locking the file. Therefore Matlab was pulling a previously saved version which was only half complete(from where I don't know, a OneDrive cache?). I forced OneDrive to finish syncing the file, now all is well. Thanks for your efforts, sorry for the run around on this one.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by