Azzera filtri
Azzera filtri

replacing numbers in columns

3 visualizzazioni (ultimi 30 giorni)
Michael
Michael il 29 Apr 2011
i have this data in columns of cell arrays that i need to save. the problem is that every so often there is a break in the data and another 3 columns include the data i need. see below.
[blank cell] [blank cell] [blank cell] [*data cell] [*data cell] [*data cell]
[blank cell] [blank cell] [blank cell] [*data cell] [*data cell] [*data cell]
[*data cell] [*data cell] [*data cell] [blank cell] [blank cell] [blank cell]
[blank cell] [blank cell] [blank cell] [*data cell] [*data cell] [*data cell]
[blank cell] [blank cell] [blank cell] [*data cell] [*data cell] [*data cell]
if you notice that the third row contains the needed data cells in the first 3 columns, rather than the last 3. there will be a couple instances of this when put all the data together. i basically need to find all the blank cells in the last 3 columns and replace them with the corresponding data cells in the first 3 columns, and then store those so that all the data can easily be read into 3 columns, with no confusion of having some data in separate columns
any ideas how this can be done?
thanks
  1 Commento
Patrice Tscherrig
Patrice Tscherrig il 29 Apr 2011
simpe (but not efficient) - use
tf = cellfun(@isempty,you_array)
--> then you can check for false in the first column. This gives you the rows where to problem occurs. I don't know how large your array is. But if it does not need to be very fast --> just loop.

Accedi per commentare.

Risposte (2)

Laura Proctor
Laura Proctor il 29 Apr 2011
If your array is named A, you can apply the function ISEMPTY with CELLFUN to determine which cells are empty. If you know that your end result needs to be three columns, then try this:
la = cellfun(@isempty,A);
nc = reshape(A(~la),[],3);

Matt Fig
Matt Fig il 29 Apr 2011
Laura's solution will not preserve the order. To preserve the order, do a transpose first. Here is an example:
% Sample Data...
A = {[] [] [] magic(2)*1 magic(2)*2 magic(2)*3;
[] [] [] magic(2)*4 magic(2)*5 magic(2)*6;
magic(2)*7 magic(2)*8 magic(2)*9 [] [] [];
[] [] [] magic(2)/1 magic(2)/2 magic(2)/3;
magic(2)/4 magic(2)/5 magic(2)/6 [] [] [];
[] [] [] magic(2)/7 magic(2)/8 magic(2)/9}
Anew = A.';
Anew = reshape(Anew(~cellfun('isempty',Anew)),3,[]).'
This makes it so that it is like the data in the first three columns was slid over to the last three columns and then the first three columns were deleted.
But I have to wonder if this is what you really want. You say at the end that you want the data to be easily read into 3 columns. Does this mean that you are going to convert the contents of the cell into an array anyway? If so, please tell us what size the data in each cell is, and if it is all the same size.

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by