How to delete empty spaces in matrix?
이전 댓글 표시
So I have been trying to remove the empty spaces created by the matrix not selected. The matrix (not shown in code) is identified as MATERIALS(:,:,:). So I tried using:
Y(cellfun('isempty',Y))= [];
but it continues to tell me cellfun works only on cells. Since it said that, I was wondering how to convert all the data into cell arrays but I do not think that is the case.
So, is if there is a way to remove empty spaces from the matrix?
also I was thinking that char (to produce vertical data) was converting the matrix into character array. So would you have to convert that into cell arrays?
so this is part of the code. If it helps.
MATERIALS{1,1,1} = 'KAOWOOL';
MATERIALS{1,1,2} = 'AL';
MATERIALS{1,1,3} = 'PMMA';
MATERIALS{1,2,3} = 'RPMMA';
MATERIALS{1,1,4} = 'HIPS';
MATERIALS{1,2,4} = 'RHIPS';
MATERIALS{1,1,5} = 'Kydex';
MATERIALS{1,2,5} = 'RKydex';
MATERIALS{1,1,6} = 'CB_A';
MATERIALS{1,2,6} = 'RCB_A';
MATERIALS{1,1,7} = 'PEI';
MATERIALS{1,2,7} = 'RPEI';
MATERIALS{1,1,8} = 'PET';
MATERIALS{1,2,8} = 'RPET';
MATERIALS{1,1,9} = 'POM';
MATERIALS{1,2,9} = 'RPOM';
MATERIALS{1,1,10} = 'ABS';
MATERIALS{1,2,10} = 'RABS';
MATERIALS{2,1,11} = 'MIXTURES';
index_selected = get(handles.cmp_list,'Value');
NI = size(index_selected,2);
Matl_index=[10, 2, 6, 4, 1, 5, 7, 8, 3, 9];
for j = 1:NI %Thermodynamics
a{j}=char(MATERIALS{:,1,Matl_index(index_selected(j))});
end
b=char(MATERIALS{:,1,11}); %Mixtures
for j = 1:NI %Kinematics
c{j}=char(MATERIALS{:,2,Matl_index(index_selected(j))});
end
X = char(a);
Z = char(c);
Y = char(X,b,Z);
% C = cellstr(Y); %testing
% C(~cellfun('isempty',C)) = []; %testing
FileName = uiputfile('*.cmp','Save as');
dlmwrite(FileName,Y,'delimiter', '', 'newline', 'pc');
댓글 수: 2
Guillaume
2014년 11월 20일
A matrix cannot have empty elements. If it's a matrix of char, each element is a single character, some of which can be the space character (ASCII 32) but never empty.
Because, by definition a matrix is square, you cannot remove elements (unless you remove a whole row or column) without reshaping it.
So, you need to better define what you mean by empty spaces ( ' ' or '' ?) and what you mean to obtain.
Your example code is not very useful without the input variables. What would be more useful is a simplified example input matrix and the expected result.
Tamfor Dulin
2014년 11월 20일
편집: Tamfor Dulin
2014년 11월 20일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!