How to delete empty spaces in matrix?
24 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Tamfor Dulin
el 20 de Nov. de 2014
Comentada: Tamfor Dulin
el 20 de Nov. de 2014
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 comentarios
Guillaume
el 20 de Nov. de 2014
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.
Respuesta aceptada
Guillaume
el 20 de Nov. de 2014
So, if I understood correctly, what you want to do is remove the blank lines in your Y char array. This is basically removing the rows of the matrix where all columns are equal to ' '. This achieved with:
Y(all(Y == ' ', 2), :) = [];
However, those blank lines are due to the fact that your MATERIALS cell array is a 2*y*z array where most of the (2,:,:) (second row) cells are empty. You then convert both rows of column y and page z into a char array, so the empty row is expanded to spaces.
That would be solved by doing:
a{j} = char(MATERIALS{1,1,Matl_index(index_selected(j))});
instead of
a{j} = char(MATERIALS{:,1,Matl_index(index_selected(j))});
Since you only get one element of the cell array, you could just as well remove the conversion to char:
a{j} = MATERIALS{1,1,Matl_index(index_selected(j))};
And actually you get rid of the for loop and just do:
a = MATERIALS(1, 1, Matl_index(index_selected));
Same for b (with 2nd row) and c (with 1st row again).
I assume you're doing this conversion to char because you want to use dlmwrite to write your text. dlmwrite is not really meant for text writing (char is actually not listed as a supported type), so you're abusing the syntax a bit. You would probably be better off to write your text file with low level functions (fopen, fprintf, fclose), There would be no need to convert to char and you'd have more control over the formatting of the file.
1 comentario
Más respuestas (0)
Ver también
Categorías
Más información sobre Biological and Health Sciences en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!