MATLAB Answers

Problem : Data Plotting from Excel

10 views (last 30 days)
Christie Latukolan
Christie Latukolan on 30 Apr 2020 at 17:00
Commented: Adam Danz on 30 Apr 2020 at 17:57
Hello, I tried to do data plotting from Excel then grouped it into 4 groups. But the problem is when I try to rename variable for the group, my plot data becomes chaotic. I attach the code below and the result. I think the problem is with the way I declare variables for groups. Please help me to solve this. Thank you very much
points = xlsread('allfreqdata.xlsx', 'Sheet9', 'I1:J1489');
G1=[]; G2=[]; G3=[]; G4=[]; %Group
for n=1:length(points)
if points(n,2)>0
if points(n,1)>0
G2(n,:) = points(n,:);
G1(n,:) = points(n,:);
if points(n,2)<0
if points(n,1)<0
G3(n,:) = points(n,:);
G4(n,:) = points(n,:);
plot (G1,'ro')
hold on;plot(G2,'bo')
hold on;plot (G3,'mo')
hold on;plot (G4,'go');grid on; axis equal;
hold off

  1 Comment

Adam Danz
Adam Danz on 30 Apr 2020 at 17:57
1) Avoid using the length() function. Length returns the longest dimension which could be the number of columns or the num of rows - you'll never know. Use size(points,2) or size(points,1) instead.
2) pre-allocate your loop variables. Instead of G1=[] use G1=nan(size(points)) or use zeros(...). Same with the other G variables.
3) Your condtions do not account for point()=0. That's why you've got a horizontal green line segment at y=0.
4) Use the debugger feature to step through your code to see what it's doing. It's obviously not doing what you intend it to do any by stepping through the code, you can pinpoint where the problem is.
5) (This is the only part of the list that needs a response) What is the intention of the for-loop and all of the conditionals? What's the big-picture of this code? It can very likely be simplified.

Sign in to comment.

Answers (0)

Sign in to answer this question.

Translated by