matlab scatter with different colors by reading a file data

1 vue (au cours des 30 derniers jours)
호철 신
호철 신 le 5 Sep 2021
Modifié(e) : 호철 신 le 5 Sep 2021
Hi. I am just a pure beginner of Matlab. I want to use Matlab scatter with different colors by reading a file data.
For example, consider that a.txt file has 6X6 data like following.
10, 20, 30 , 40, 50, 60
14 , 18, 17, 26, 39 ,11
red, blue, red, red, blue, green
10, 20, 30 , 40, 50, 60
19 , 88, 13, 7, 56 ,21
black, red , grey, black, black, red
First,second, the forth, the fifth lows are number. The third and the sixth lows are text.
Now I want to make at first 6 dots on 2-dimension screen with 3 differe colors. (red, blue, green) and (with pause) the next 6 dots on 2-dimension screen with 3 differe colors. (black, grey, red)
First and forth lows are the x value and second and fifth lows are the y value the dots of x-y plane.
How can I code matlab for this problem?
I tried make the code like this for the three lows.
fileID1 = fopen('a.txt','r');
formatSpec = '%f %f %s';
sizeA = [3 Inf];
A = fscanf(fileID1,formatSpec,sizeA)
x=A(1,:)
y=A(2,:)
z=A(3,:)
scatter(x,y,20,z,'filled')
But this failed because I don't know the exact usage of 'formatSpec'. Also I don't know how break the 6 lows with 3X6 matrix.
I hope your help.

Réponse acceptée

Walter Roberson
Walter Roberson le 5 Sep 2021
Modifié(e) : Walter Roberson le 5 Sep 2021
Break it up into several pieces:
cnames = {'black', 'blue', 'green', 'grey', 'red'};
cmap = [
0 0 0; %black
0 0 1; %blue
0 1 0; %green
.5 .5 .5; %grey
1 0 0; %red
];
fileID1 = fopen('a.txt','r');
while true
tx = fgetl(fileID1);
ty = fgetl(fileID1);
tc = fgetl(fileID1);
if ~ischar(tx) || ~ischar(ty) || ~ischar(tc); break; end %end of file
x = sscanf(tx, '%f,', [1 inf]);
y = sscanf(ty, '%f,', [1 inf]);
innames = regexp(tc, '\s*,\s*', 'split');
[found, idx] = ismember(innames, cnames);
if ~all(found)
error('color "%s" is unknown', innames{find(~found,1)});
end
c = cmap(idx,:);
scatter(x, y, 200, c, 'filled');
end
  1 commentaire
호철 신
호철 신 le 5 Sep 2021
Modifié(e) : 호철 신 le 5 Sep 2021
It works! great. Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Time Series Events dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by