Conversion to cell from char is not possible

33 次查看(过去 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
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';
Josh G
Josh G 2015-7-14
编辑:Josh G 2015-7-14
Big edit to this comment. I was being dumb and using "Run and advance" instead of running the script with the Run option. That gave a line number and I fixed the problem. However, there is a new problem. Here is the entire script:
%Name Converter
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 = 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
xlswrite(filepath, names, safeRange);
input('All done. Press any key to end.');
For an example for names, use
names = {'JOHNSON MURPHY B'; 'MADISON LEE & LAINE C'; 'MAXWELL THOMAS ALBERT & MARIA S'}
The error is
Error using strjoin (line 52)
First input must be a 1xN cell array of strings.
Error in nameSwitcher (line 29)
names{i} = strjoin(stringWords);
But size(stringWords') = [1 6], and it is a cell array of strings.

请先登录,再进行评论。

采纳的回答

Image Analyst
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 个评论
Josh G
Josh G 2015-7-15
That's odd. I tried the script you posted and it works exactly as described. When I try to read from the spreadsheet I have it tells me the error is in strjoin though. I'll keep working on it, but the particular problem I asked about in this question is fixed, so I'll go ahead and mark it as answered. Thank you!

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
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

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by