cell array into vector of strings
1 次查看(过去 30 天)
显示 更早的评论
Greetings,
I am having a great deal of difficulty importing text data from excel into a vector of strings that I can index.
I am using
data = importdata('data.xls')
to get a struct with one cell [192 x 1 cell]
to access a specific string I use
data.Sheet1{1}
returns
'236C7 '
I cannot directly convert this to deicmal due to the space at the end. The code I am using to scrub the data of that is below
c_limit = size(data.Sheet1);
data1 = zeros(cal_limit,1);
for ii = 1:c_limit
temp_c_string = data.Sheet1{ii};
length_o_value = size(temp_cal_string);
for hh = 1:length_o_value
if isspace(temp_c_string(hh))== true
temp_c_string(hh) = [];
end
data1(ii)= data.Sheet1{ii};
end
end
It keeps giving me an error that proclaims that I am referencing a number out of matrix dimension. I know this but I am referring to a string and it seems to not want to let me..
is there a better way to get the dat from this cell into a vector of strings? or an array of strings?
Thanks
0 个评论
回答(1 个)
Walter Roberson
2012-8-13
See http://www.mathworks.co.uk/matlabcentral/answers/45873-evaluation-of-a-for-loop for why your loop is failing.
1 个评论
Walter Roberson
2012-8-13
His code has a few other mistakes, such as using size() instead of length(), and not using the value of temp_c_string after it is computed. But overall your solution was probably the correct one, Lucas, to use deblank()
for ii = 1 : length(data.Sheet1)
data1(ii) = deblank(data.Sheet1{ii});
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!