Conversion to cell from char is not possible
이전 댓글 표시
I'm writing a script that will read a set of names from a spreadsheet, make all letters in the names lowercase, capitalize the first letter of each new name, and move the first name to the end of the string, then write the new set of names to the spreadsheet. So, for example, GLENN CURTISS T & MYRA B becomes Curtiss T & Myra B Glenn.
[nothing, names] = xlsread(filepath, sheet, range);
names = lower(names);
for i = 1:length(names)
stringWords = strread(names{i}, '%s'); %create an array of each "word" in a name
for j = 1:length(stringWords)
charWords = char(stringWords{j}); %for each word, convert it into a character array
charWords(1) = upper(charWords(1)); %capitalize the first letter of the working word
stringWords{j} = cellstr(charWords); %write edited string back to stringWords
end
% rest of the program
%
%
end
This gives me an error saying "Conversion to cell from char is not possible." My understanding of this code is that charWords is a char array identical to the string stored in cell j of stringWords. After capitalizing the first letter, cellstr(charWords) should convert the string made up by charWords into a cell that can be returned to stringWords. What am I doing wrong?
댓글 수: 5
Josh G
2015년 7월 14일
the cyclist
2015년 7월 14일
The complete error message would include the line number, etc.
Even better would be for you to put in an example of nothing and names that reproduces the error, so that we have a self-contained code snippet that we can run.
It is possible that the error message only contains that one line, as this demonstrates:
>> A = {3}; % note: a cell array!
>> A(1) = 'b'
Conversion to cell from char is not possible.
but this can only happen at the command line. Whereas in a script or function, e.g.:
for k = 1:3
A = {k}; % cell!
A(k) = 'b';
end
the error message will always give the Mfile name, the line number and also quotes the code where the problem is detected:
>> Untitled
Conversion to cell from char is not possible.
Error in Untitled (line 4)
A(k) = 'b';
채택된 답변
추가 답변 (1개)
Image Analyst
2015년 7월 14일
0 개 추천
I think you will understand after you read the FAQ, which gives a good intuitive description of cells and when to use braces or parentheses. http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!