help extracting data from cell arrays
1 次查看(过去 30 天)
显示 更早的评论
Hi, i have a set of data within a cell array which can vary put looks like this
col1---col2---col3---col4 smpA smpB smpC smpD smpE smpF
i need to extract this data and store it into a database but within one cell, as each col represents either TN-TP-FP-FN i decided to number each data entry using this:
[p,q]=size(MATRIX); % Get dimensions of MATRIX
for j = 1:q % Number of columns in MATRIX
for i = 1:p % Number of rows in MATRIX
Temp = char(MATRIX(i,j)); % Extract value at i,j
if ~isempty(Temp)
if (j == q && i == p && j ~= 1 && i ~= 1)
STRING = [STRING Temp int2str(j)]; % Last entry
elseif (j == 1 && i == 1)
STRING = [Temp int2str(j) ',']; % First entry
else
STRING = [STRING Temp int2str(j) ',']; % Other entries
end
end
end
end
% Check for presence of last comma
if strcmp(STRING(end),',') == 1
STRING=STRING(1:end-1); %Omit last character which could be comma
end
This works fine and gives something like smpA1,smpB2,smpE2,smpC3,smpF3,smpD4
however when there is a set of samples which were not used to generate data it obviously crashes
col1---col2---col3---col4
smpB smpD
smpE
I tried something like this:
[p,q]=size(MATRIX); % Get dimensions of MATRIX
for j = 1:q % Number of columns in MATRIX
for i = 1:p % Number of rows in MATRIX
Temp = char(MATRIX(i,j)); % Extract value at i,j
if ~isempty(Temp)
if (j == q && i == p && j ~= 1 && i ~= 1)
STRING = [STRING Temp int2str(j)]; % Last entry
elseif (j == 1 && i == 1)
STRING = [Temp int2str(j) ',']; % First entry
else
STRING = [STRING Temp int2str(j) ',']; % Other entries
end
* else isempty(Temp)
if (j == q && i == p && isempty(j) == 1 && isempty(i) ==1)
STRING = [STRING Temp ','];*
end
end
end
if strcmp(STRING(end),',') == 1 STRING=STRING(1:end-1); end
i get the following error
??? Undefined variable or function STRING might refer to the function string.
Error in ==> cellmatrix2string2 at 44 STRING = [STRING Temp int2str(j) ',']; % Other entries
anyone know why this is occuring, and a way around it?
0 个评论
回答(1 个)
Image Analyst
2013-8-15
I didn't look over your algorithm in detail but it looks like you can define STRING in a way that requires an existing value for STRING:
STRING = [STRING Temp int2str(j)];
but I didn't see you initialize STRING. So maybe put
STRING = '';
at the beginning of your code.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Database Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!