필터 지우기
필터 지우기

help extracting data from cell arrays

조회 수: 1 (최근 30일)
Gurvinder
Gurvinder 2013년 8월 15일
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?

답변 (1개)

Image Analyst
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.
  댓글 수: 1
Gurvinder
Gurvinder 2013년 8월 15일
ah sorry i didnt upload the full part of the code, at the top of the function i have:
function STRING = cellmatrix2string2(MATRIX)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by