Looping elseif conditions to process .csv data

1 view (last 30 days)
Question: I have bulk of data for nodes 4-12. I would like to allocate lines from a .csv file to specific arrays 4-12 based on the node# which appears in column 1 of the .csv. The .csv file has 8 columns in all. Looking at my code below, I must type out each elseif condition manually for the conditions M(i,1)==3 all the way until M(i,1)==12. Is there a way to loop the elseif for values from M(i,1)==3, M(i,1)==4,...,M(i,1)==12 without causing a ruckus? I believe I need another layer of loops to do so.
Prospective Code :
M = csvread(fileName,1,0);
counter = [zeros(1,16)]; % [zeros(2,17)] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0] == 1x16 matrice of zeros
Node = zeros(1,16); % initializes node variables... Node(1)=0, Node(2)=0,...,Node(16)=0.
for i = 1 : size(M,1)
if M(i,1)==2 % if element in matrix M at row i, col 1 = 2
counter(M(i,1)) = counter(M(i,1))+1; % M(i,1)=2, so counter(2)=counter(2)+1
Node{M(i,1)}(counter(M(i,1)),i:8) = M(i,1:8);
elseif M(i,1)==3
counter(M(i,1)) = counter(M(i,1))+1;
Node{M(i,1)}(counter(M(i,1)),i:8) = M(i,1:8);
elseif M(i,1)==4
counter(M(i,1)) = counter(M(i,1))+1;
Node{M(i,1)}(counter(M(i,1)),i:8) = M(i,1:8);
elseif M(i,1)==5
counter(M(i,1)) = counter(M(i,1))+1;
Node{M(i,1)}(counter(M(i,1)),i:8) = M(i,1:8);
elseif M(i,1)==6
counter(M(i,1)) = counter(M(i,1))+1;
Node{M(i,1)}(counter(M(i,1)),i:8) = M(i,1:8);
elseif M(i,1)==7
counter(M(i,1)) = counter(M(i,1))+1;
Node{M(i,1)}(counter(M(i,1)),i:8) = M(i,1:8);
elseif M(i,1)==8
counter(M(i,1)) = counter(M(i,1))+1;
Node{M(i,1)}(counter(M(i,1)),i:8) = M(i,1:8);
elseif M(i,1)==9
counter(M(i,1)) = counter(M(i,1))+1;
Node{M(i,1)}(counter(M(i,1)),i:8) = M(i,1:8);
elseif M(i,1)==10
counter(M(i,1)) = counter(M(i,1))+1;
Node{M(i,1)}(counter(M(i,1)),i:8) = M(i,1:8);
elseif M(i,1)==11
counter(M(i,1)) = counter(M(i,1))+1;
Node{M(i,1)}(counter(M(i,1)),i:8) = M(i,1:8);
elseif M(i,1)==12
counter(M(i,1)) = counter(M(i,1))+1;
Node{M(i,1)}(counter(M(i,1)),i:8) = M(i,1:8);
end
end
  8 Comments
Guillaume
Guillaume on 26 Jul 2019
Edited: Guillaume on 26 Jul 2019
Well, yes, I'm sorry to say that code is awful.
Numbered variables are always a mistake. The correct way is to index the variables. I think that's what you're trying to remediate with your new code.
Again, if you end up writing the same lines several time with only minor variations, you're doing it wrong. The computer should be doing the repetition for you. That's what they're good at.
As there's too much code for us to try to understand what it's doing (the lack of comments explaining what it's meant to do doesn't help), it would probably be a better use of our time if we ignored it and you explained in details what you want to do with the input data.
And also, what version of matlab are you using?
dpb
dpb on 26 Jul 2019
Can't emphasize Guillaume's comments enough (and others to same effect earlier).
Just explain what the data are in the file (no headers there doesn't help) and what it is you want to do with it. Give the basic definitions of the conversion explaining which variable is which and what units apply and then what is to be plotted against what. The starting point will be something like
t=readtable('Lods June13.2019 AB.csv');
t.Properties.VariableNames={'Sensor', ??? };
[g,id]=findgroups(t.Sensor);
then to apply the various operations to the groups as identified...see the doc for splitapply as one way to apply a specific function; simply a loop over the number of groups calling a function may be the ticket here with the plotting and all...

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!