Plotting variables of continuously array with different colors
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hello everyone,
Is it possible to :
(1) Change the value of Station column of this array (excel attached) into ordered rank Station? Anyone knows the code to do this? FYI, the number of stations is in hundreds (I put here for example only 3 stations) so, doing this in loop is preferable solution.

(2) Plot those three Station's profile of variable "s" vs depth from those three stations with different colors of line plots? Anyone knows the code to do this also?
Thank you!
채택된 답변
Star Strider
2023년 10월 10일
편집: Star Strider
2023년 10월 10일
T1 = readtable('datax.xlsx')
T1 = 17×5 table
Station lon lat depth salinity
_______ ______ _____ _____ ________
2 114.64 -8.16 0 33.995
2 114.64 -8.16 2 33.994
2 114.64 -8.16 4 33.994
2 114.64 -8.16 6 33.993
2 114.64 -8.16 8 33.993
2 114.64 -8.16 10 33.992
7 114.72 -8.16 0 33.976
7 114.72 -8.16 2 33.978
7 114.72 -8.16 4 33.977
7 114.72 -8.16 6 33.977
31 114.8 -8.16 0 33.983
31 114.8 -8.16 2 33.981
31 114.8 -8.16 4 33.981
31 114.8 -8.16 6 33.981
31 114.8 -8.16 8 33.981
31 114.8 -8.16 10 33.98
[Us,ix1,ix2] = unique(T1.Station, 'stable');
T1.Station = ix2
T1 = 17×5 table
Station lon lat depth salinity
_______ ______ _____ _____ ________
1 114.64 -8.16 0 33.995
1 114.64 -8.16 2 33.994
1 114.64 -8.16 4 33.994
1 114.64 -8.16 6 33.993
1 114.64 -8.16 8 33.993
1 114.64 -8.16 10 33.992
2 114.72 -8.16 0 33.976
2 114.72 -8.16 2 33.978
2 114.72 -8.16 4 33.977
2 114.72 -8.16 6 33.977
3 114.8 -8.16 0 33.983
3 114.8 -8.16 2 33.981
3 114.8 -8.16 4 33.981
3 114.8 -8.16 6 33.981
3 114.8 -8.16 8 33.981
3 114.8 -8.16 10 33.98
figure
stem3(T1.Station, T1.depth, T1.salinity, ':k', 'Marker','none')
hold on
scatter3(T1.Station, T1.depth, T1.salinity, 150, T1.salinity, 'p', 'filled')
hold off
xlabel('Station')
ylabel('Depth')
zlabel('Salinity')
xticks(1:3)

This uses the third output of unique to get serial indices (note that I use the 'stable' argument to avoid sorting the results). The findgroups function works similarly to produce a grouping variable. The grouping variable vector is used here as the new value for ‘Station’.
EDIT — Added plots.
.
댓글 수: 12
+1, unique/findgroups is the way to go here.
Adi Purwandana
2023년 10월 11일
편집: Adi Purwandana
2023년 10월 11일
Anyway, for the 2nd question, is it possible to get a simple line plot of all stations (not in 3D plot as your suggestion) with color difference, for example, in jet color (e.g., Station 1-red,....., station 3-blue)?
Yes, use a for loop to go through each station and plot the data and setting the plot color corresponding to each station.
yeah, that's my problem... could you please @Dyuman Joshi write a piece of codes to do that? I don't get it if the stations are located continously in one column.
I am not certain what you want to plot.
Plotting ‘salinity’ here —
T1 = readtable('datax.xlsx')
T1 = 17×5 table
Station lon lat depth salinity
_______ ______ _____ _____ ________
2 114.64 -8.16 0 33.995
2 114.64 -8.16 2 33.994
2 114.64 -8.16 4 33.994
2 114.64 -8.16 6 33.993
2 114.64 -8.16 8 33.993
2 114.64 -8.16 10 33.992
7 114.72 -8.16 0 33.976
7 114.72 -8.16 2 33.978
7 114.72 -8.16 4 33.977
7 114.72 -8.16 6 33.977
31 114.8 -8.16 0 33.983
31 114.8 -8.16 2 33.981
31 114.8 -8.16 4 33.981
31 114.8 -8.16 6 33.981
31 114.8 -8.16 8 33.981
31 114.8 -8.16 10 33.98
[Us,ix1,ix2] = unique(T1.Station, 'stable');
T1.Station = ix2
T1 = 17×5 table
Station lon lat depth salinity
_______ ______ _____ _____ ________
1 114.64 -8.16 0 33.995
1 114.64 -8.16 2 33.994
1 114.64 -8.16 4 33.994
1 114.64 -8.16 6 33.993
1 114.64 -8.16 8 33.993
1 114.64 -8.16 10 33.992
2 114.72 -8.16 0 33.976
2 114.72 -8.16 2 33.978
2 114.72 -8.16 4 33.977
2 114.72 -8.16 6 33.977
3 114.8 -8.16 0 33.983
3 114.8 -8.16 2 33.981
3 114.8 -8.16 4 33.981
3 114.8 -8.16 6 33.981
3 114.8 -8.16 8 33.981
3 114.8 -8.16 10 33.98
StaData = accumarray(T1.Station, (1:size(T1,1)).', [], @(x){T1(x,:)})
StaData = 3×1 cell array
{6×5 table}
{4×5 table}
{7×5 table}
n = numel(StaData);
colororder(jet(n))
cm = colormap(jet(n));
figure
hold on
for k = 1:n
% StaData{k}
scatter(StaData{k}.Station, StaData{k}.salinity, 50, cm(k,:), 's', 'filled')
end
% scatter3(T1.Station, T1.depth, T1.salinity, 150, T1.salinity, 'p', 'filled')
hold off
xlabel('Station')
ylabel('Salinity')
% ylabel('Depth')
% zlabel('Salinity')
xticks(1:3)
xlim([min(xticks)-1 max(xticks)+1])
colormap(jet(n))
hcb = colorbar;
hcb.Ticks = (1:n)/(n);
hcb.TickLabels = 1:n;
hcb.Label.String = 'Stations';

This approach creates individual table arrays for each station in the file, and plots them in a scatter plot.
.
Adi Purwandana
2023년 10월 11일
편집: Adi Purwandana
2023년 10월 11일
Sorry for confusing you @Star Strider, actually I want to plot it as the comment from @Sulaymon Eshkabilov below. But, since the stations can be many, I need any other way. So, is it possible to modify your code so that I can get the plot as the one plotted by @Sulaymon Eshkabilov below?
I do not know what you want to do.
Using my code, it is relatively straightforward to plot anything against anything —
T1 = readtable('datax.xlsx')
T1 = 17×5 table
Station lon lat depth salinity
_______ ______ _____ _____ ________
2 114.64 -8.16 0 33.995
2 114.64 -8.16 2 33.994
2 114.64 -8.16 4 33.994
2 114.64 -8.16 6 33.993
2 114.64 -8.16 8 33.993
2 114.64 -8.16 10 33.992
7 114.72 -8.16 0 33.976
7 114.72 -8.16 2 33.978
7 114.72 -8.16 4 33.977
7 114.72 -8.16 6 33.977
31 114.8 -8.16 0 33.983
31 114.8 -8.16 2 33.981
31 114.8 -8.16 4 33.981
31 114.8 -8.16 6 33.981
31 114.8 -8.16 8 33.981
31 114.8 -8.16 10 33.98
[Us,ix1,ix2] = unique(T1.Station, 'stable');
T1.Station = ix2
T1 = 17×5 table
Station lon lat depth salinity
_______ ______ _____ _____ ________
1 114.64 -8.16 0 33.995
1 114.64 -8.16 2 33.994
1 114.64 -8.16 4 33.994
1 114.64 -8.16 6 33.993
1 114.64 -8.16 8 33.993
1 114.64 -8.16 10 33.992
2 114.72 -8.16 0 33.976
2 114.72 -8.16 2 33.978
2 114.72 -8.16 4 33.977
2 114.72 -8.16 6 33.977
3 114.8 -8.16 0 33.983
3 114.8 -8.16 2 33.981
3 114.8 -8.16 4 33.981
3 114.8 -8.16 6 33.981
3 114.8 -8.16 8 33.981
3 114.8 -8.16 10 33.98
StaData = accumarray(T1.Station, (1:size(T1,1)).', [], @(x){T1(x,:)})
StaData = 3×1 cell array
{6×5 table}
{4×5 table}
{7×5 table}
n = numel(StaData);
colororder(jet(n))
cm = colormap(jet(n));
figure
hold on
for k = 1:n
% StaData{k}
plot(StaData{k}.depth, StaData{k}.salinity, '-s', 'Color',cm(k,:), 'MarkerSize',10, 'MarkerEdgeColor',cm(k,:), 'MarkerFaceColor',cm(k,:))
end
% scatter3(T1.Station, T1.depth, T1.salinity, 150, T1.salinity, 'p', 'filled')
hold off
xlabel('Depth')
ylabel('Salinity')
% ylabel('Depth')
% zlabel('Salinity')
% xticks(1:3)
xlim([min(xticks)-1 max(xticks)+1])
colormap(jet(n))
xlim([min(xlim)-1 max(xlim)+1])
hcb = colorbar;
hcb.Ticks = (1:n)/(n)-(1/(2*n));
hcb.TickLabels = 1:n;
hcb.Label.String = 'Stations';

.
As always, my pleasure!
Adi Purwandana
2023년 10월 11일
편집: Adi Purwandana
2023년 10월 11일
@Star Strider Anyway...my last question to this thread... could you please help me to define LON, that is the value of lon from each StaData?
for k = 1:n
% StaData{k}
LON = StaData{k}.lon; % I want to pick lon value from each table of StaData and store it as LON
end
FYI, I can solve if it is not a continous array. I do this code when running multi excel files:
for k = 1:n
filename = A(k).name;
data = readmatrix(filename);
LON(k,1)= data(1,2); %pick lon value from each table from each file/data and store it as LON
end
But I dont know to adapt this in your code.
Adi Purwandana
2023년 10월 11일
편집: Adi Purwandana
2023년 10월 11일
I think I solved it...
LON(k,:) = StaData{k}.long(1);
That is correct.
Actually:
LON = StaData{k}.lon(1);
or:
LON(k) = StaData{k}.lon(1);
depending on the result you want.
(I was away for a few minutes running errands.)
추가 답변 (1개)
Logical indexing would be an easy way of sorting the data and assigning new values to column 'station'. Then use the sorted data indecies to plot the data: s vs. depth
Here is one of the ways of doing it:
F = readmatrix('datax.xlsx');
Idx1=F(:,1)==2;
Idx2=F(:,1)==7;
Idx3=F(:,1)==31;
F(Idx1,1) = 1;
F(Idx2,1) = 2;
F(Idx3,1) = 3;
% Plotting the data
plot(F(Idx1, 5),F(Idx1, 4), '-o','linewidth', 2), hold all
plot(F(Idx2, 5),F(Idx2, 4), '-s','linewidth', 2)
plot(F(Idx3, 5),F(Idx3, 4), '-d','linewidth', 2)
legend('Station: 1', 'Station: 2', 'Station: 3', 'location', 'best')
grid on
xlabel('s')
ylabel('depth')

댓글 수: 1
Thank you @Sulaymon Eshkabilov. It's solved if the number of station is not much. In my case, the number of stations is in hundreds and even thousands. Do you know to do that in something like looping lines to do this?
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
