I analyse data files containing different number of columns using the same program.
Each data file requires different conditions for the reshape
function to work successfully. I have the following code which works perfectly. Briefly, if the reshape is not successful, it subtract 1 from x in raw = raw(:,1,x);
and tries to reshape, and if it still can't reshape it tries again and so on.
Is there away to rewrite this piece of code to make it less tedious?
[~, ~, raw] = xlsread(test_filename,test_sheetname);
try raw = raw(:,1:17); d = reshape([raw{:}],size(raw)); catch
try raw = raw(:,1:16); d = reshape([raw{:}],size(raw));catch
try raw = raw(:,1:15); d = reshape([raw{:}],size(raw));catch
try raw = raw(:,1:14); d = reshape([raw{:}],size(raw));catch
try raw = raw(:,1:13); d = reshape([raw{:}],size(raw));catch
try raw = raw(:,1:12); d = reshape([raw{:}],size(raw));catch
try raw = raw(:,1:11); d = reshape([raw{:}],size(raw));catch
end
end
end
end
end
end
end
raw
? Please make some random example data so we can understand the problem better. I don’t understand what your reshape statement is supposed to do. It will always work if all cell elements have a scalar, and it will never work if any cell has more than one value, unless another cell is empty. How are these data generated? Why a cell array? How come it’s OK to cut part of the data away? – Cris Luengoraw
that you can share. Are you cutting off columns with empty cells? Or with text? Any of these things can be done more easily and efficiently than just trying repeatedly like you do. Using try/catch like this or in a loop like in the answer is just a really bad design. – Cris Luengo