replacing numbers in columns
3 views (last 30 days)
Show older comments
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 Comment
Patrice Tscherrig
on 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.
Answers (2)
Matt Fig
on 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.
0 Comments
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!