0
votes

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
1
What is in 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 Luengo
Thank you for your comments, I made some changes addressing your concerns.Mosawi
This still makes no sense. Please create a small example raw 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

1 Answers

1
votes

As noted in the comments this question needs clarification. However you can use a for loop to avoid nesting try/catch statements:

for x = 17:-1:11
  try
    raw = raw(:,1:x); 
    d = reshape([raw{:}],size(raw)); 
    break;
  catch
  end
end