replacing numbers in columns
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Patrice Tscherrig
2011-4-29
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.
回答(2 个)
Matt Fig
2011-4-29
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!