Conversion to cell from char is not possible
조회 수: 34 (최근 30일)
이전 댓글 표시
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
Stephen23
2015년 7월 14일
편집: Stephen23
2015년 7월 14일
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';
채택된 답변
Image Analyst
2015년 7월 15일
Josh: This works fine. No error at all. I didn't change anything except I used the names you gave instead of reading from the workbook, and I didn't write out a workbook.
clc;
clear all;
% filepath = input('What is the file path from which you wish to read? \n', 's');
% sheet = input('What is the sheet name? \n', 's');
% range = input('What is the range of names? \n', 's');
% safeRange = input('Please give a safe range of the same dimensions to which to write. \n', 's');
% input('Make sure the file is closed before execution. Press any key to start.\n');
% [nothing, names] = xlsread(filepath, sheet, range);
names = {'JOHNSON MURPHY B'; 'MADISON LEE & LAINE C'; 'MAXWELL THOMAS ALBERT & MARIA S'}
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}); %at each word, convert it into a character array
charWords(1) = upper(charWords(1)); %capitalize the first letter of the working word
stringWords{j} = charWords; %write edited string back to stringWords
end
last = length(stringWords);
tempWord = stringWords(1);
for k = 1:length(stringWords)
if k ~= length(stringWords)
stringWords(k) = stringWords(k+1);
else
stringWords(k) = [];
end
end
stringWords(last) = tempWord;
names{i} = strjoin(stringWords');
end
names
% xlswrite(filepath, names, safeRange);
% input('All done. Press any key to end.');
Results in the command window:
names =
'JOHNSON MURPHY B'
'MADISON LEE & LAINE C'
'MAXWELL THOMAS ALBERT & MARIA S'
names =
'Murphy B Johnson'
'Lee & Laine C Madison'
'Thomas Albert & Maria S Maxwell'
댓글 수: 2
추가 답변 (1개)
Image Analyst
2015년 7월 14일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!