# What's the best way to shorten this?

2 views (last 30 days)
Hans123 on 9 May 2019
Commented: Adam Danz on 13 May 2019
This is a very lengthy code for something repetitive, I want to know whether there is a simpler, more efficient way of doing this.
The purpose of the code is to load text files and trim them according to the same criteria and check which one is the shortest and use the shortest length in calculations that happen later in the code.
%removal of unwanted elements from the raw data array
T1(T1(:,11)<=0 ,:) = [];
T1(T1(:,3)==25 ,:) = [];
T1(T1(:,3)<500 ,:) = [];
T1(T1(:,5)==0 ,:)= [];
T1(find(T1(:,12)>780,1):end, :) = [];
T2(T2(:,11)<=0 ,:) = [];
T2(T2(:,3)==25 ,:) = [];
T2(T2(:,3)<500 ,:) = [];
T2(T2(:,5)==0 ,:)= [];
T2(find(T2(:,12)>780,1):end, :) = [];
T2(T2(:,11)<=0 ,:) = [];
T3(T3(:,3)==25 ,:) = [];
T3(T3(:,3)<500 ,:) = [];
T3(T3(:,5)==0 ,:)= [];
T3(find(T3(:,12)>780,1):end, :) = [];
T4(T4(:,11)<=0 ,:) = [];
T4(T4(:,3)==25 ,:) = [];
T4(T4(:,3)<500 ,:) = [];
T4(T4(:,5)==0 ,:)= [];
T4(find(T4(:,12)>780,1):end, :) = [];
T5(T5(:,11)<=0 ,:) = [];
T5(T5(:,3)==25 ,:) = [];
T5(T5(:,3)<500 ,:) = [];
T5(T5(:,5)==0 ,:)= [];
T5(find(T5(:,12)>780,1):end, :) = [];
T6(T6(:,11)<=0 ,:) = [];
T6(T6(:,3)==25 ,:) = [];
T6(T6(:,3)<500 ,:) = [];
T6(T6(:,5)==0 ,:)= [];
T6(find(T6(:,12)>780,1):end, :) = [];
T7(T7(:,11)<=0 ,:) = [];
T7(T7(:,3)==25 ,:) = [];
T7(T7(:,3)<500 ,:) = [];
T7(T7(:,5)==0 ,:)= [];
T7(find(T5(:,12)>780,1):end, :) = [];
T8(T8(:,11)<=0 ,:) = [];
T8(T8(:,3)==25 ,:) = [];
T8(T8(:,3)<500 ,:) = [];
T8(T8(:,5)==0 ,:)= [];
T8(find(T8(:,12)>780,1):end, :) = [];
T9(T9(:,11)<=0 ,:) = [];
T9(T9(:,3)==25 ,:) = [];
T9(T9(:,3)<500 ,:) = [];
T9(T9(:,5)==0 ,:)= [];
T9(find(T9(:,12)>780,1):end, :) = [];
T10(T10(:,11)<=0 ,:) = [];
T10(T10(:,3)==25 ,:) = [];
T10(T10(:,3)<500 ,:) = [];
T10(T10(:,5)==0 ,:)= [];
T10(find(T10(:,12)>780,1):end, :) = [];
L1=length(T1);
L2=length(T2);
L3=length(T3);
L4=length(T4);
L5=length(T5);
L6=length(T6);
L7=length(T7);
L8=length(T8);
L9=length(T9);
L10=length(T10);
Lmin = min([L1(:);L2(:);L3(:);L4(:);L5(:);L6(:);L7(:);L8(:);L9(:);L10(:)]);
##### 3 CommentsShow 1 older commentHide 1 older comment
Stephen23 on 13 May 2019
Using numbered variables is a sign that you are doing something wrong.
Copy-and-pasting code is a sign that you are doing something wrong.
Remember that computers are only really good at one thing: repeatedly doing simple tasks. So when you find yourself copy-and-pasting the same code over and over again, then you are just doing the computer's job for it. Instead get the computer to do it for you: use a loop.
Hans123 on 13 May 2019
thank you for your inputs, I really appreciate it!

Adam Danz on 9 May 2019
Edited: Adam Danz on 13 May 2019
This should get you started but it's not tested and there will likely be some kinks to work out. I'd be glad to help more if needed.
trials = ["Trial 1", "Trial 2", "Trial 3", "Trial 4", "Trial 5", ...
"Trial 6", "Trial 7", "Trial 8", "Trial 9", "Trial 10" ];
nTrials = numel(trials);
T = cell(size(trials));
for i = 1:nTrials
%removal of unwanted elements from the raw data array
T{i}(T{i}(:,11)<=0 ,:) = [];
T{i}(T{i}(:,3)<500 ,:) = [];
T{i}(T{i}(:,5)==0 ,:)= [];
T{i}(find(T{i}(:,12)>780,1):end, :) = [];
end
L = cellfun(@length, T,'UniformOutput', false);
Lmin = min([L{:}]);
Hans123 on 13 May 2019
this works perfectly, I really appreciate the help. I am relatively new to MATLAB and cell functions as well. I didn't quite understand how this line works
L = cellfun(@length, T,'UniformOutput', false);
Any help would be appreciated.
Cheers
Adam Danz on 13 May 2019
The first input to cellfun() is a function handle. The second input is a cell array. The cellfun() function applies the function in input #1 to each element of the cell array in input #2. For example, the cell array T (defined below) has 4 elements. The length of the four elements are [3, 4, 5, 2].
T = { [1 2 3], [1 2 3 4], [1 2 3 4 5], [1 2] };
The 'UniformOutput' flag specifies that the output should be stored in a cell array.

Kevin Phung on 9 May 2019