Problem : Data Plotting from Excel

2 vues (au cours des 30 derniers jours)
Merlyn Inova
Merlyn Inova le 30 Avr 2020
Commenté : Adam Danz le 30 Avr 2020
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,:);
else
G1(n,:) = points(n,:);
end
else
if points(n,2)<0
if points(n,1)<0
G3(n,:) = points(n,:);
else
G4(n,:) = points(n,:);
end
end
end
end
plot (G1,'ro')
hold on;plot(G2,'bo')
hold on;plot (G3,'mo')
hold on;plot (G4,'go');grid on; axis equal;
hold off
xlabel('X');
ylabel('Y');
  1 commentaire
Adam Danz
Adam Danz le 30 Avr 2020
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.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by