Filter löschen
Filter löschen

How to assign legend and marker to a 2D graph in Matlab?

2 Ansichten (letzte 30 Tage)
Behrad Ze
Behrad Ze am 21 Aug. 2022
Kommentiert: Star Strider am 21 Aug. 2022
I have an excel file consiting of multiple columns. The first column is X and the rest of columns are Y. The first row is also the titles. I am reading the excel file in Matlab and plotting them in a figure. I am using the code as below. I would like to assign different markers and legends to each Y, as it is defined in the first row. I have also attached the sample csv file. Would anyone help me in this regard? I am using the code as below. Thank You so much in advance.
csvfiles = dir('*.csv');
for file = csvfiles'
sample=readmatrix(file.name);
x=sample(1:end,1);
dataSample=sample(1:end,2:end);
figure
plot(x,dataSample(1:end,:)','--')
end

Akzeptierte Antwort

Star Strider
Star Strider am 21 Aug. 2022
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1103385/sample.csv')
T1 = 1868×33 table
Var1 C0 C1 C2 C3 C4 C5 C6 C7 H0 H1 H2 H3 H4 H5 H6 H7 R0 R1 R2 R3 R4 R5 R6 R7 Z0 Z1 Z2 Z3 Z4 Z5 Z6 Z7 ______ _________ _________ ________ ________ ________ _________ _________ _________ _________ _________ ________ _________ _________ _________ _________ ________ _________ ________ ________ ________ ________ ________ _________ _________ ________ _________ _________ _________ _________ _________ _________ _________ 4000.4 0.0030977 0.0068682 0.010064 0.013078 0.012625 0.0091874 0.00734 0.0096967 0.0082328 0.0084078 0.015286 0.0034295 0.0026153 0.0036699 0.0087307 0.012609 0.0016383 0.011388 0.010033 0.012429 0.01759 0.011811 0.0078433 0.008815 0.01212 0.006157 0.0086189 0.0067927 0.0058852 0.0046381 0.0044672 0.0048745 3998.4 0.0031007 0.0068682 0.010058 0.013089 0.012625 0.0091916 0.007349 0.0096967 0.0082328 0.0083902 0.015284 0.0034215 0.0026231 0.0036668 0.0087243 0.012611 0.0016342 0.011406 0.010041 0.012429 0.017658 0.011811 0.0078433 0.008815 0.012127 0.006157 0.0086171 0.0067927 0.0058852 0.0046435 0.0044798 0.0048745 3996.5 0.0031007 0.0068733 0.010058 0.01309 0.012605 0.0091966 0.00734 0.0097045 0.008242 0.0083902 0.015331 0.0034215 0.0026231 0.0036447 0.0087158 0.012608 0.0016318 0.011381 0.010048 0.012511 0.017746 0.011802 0.0078433 0.008815 0.012137 0.0061542 0.0086133 0.0067927 0.0058889 0.0046435 0.0044718 0.0048527 3994.6 0.0031041 0.006879 0.010052 0.013082 0.012605 0.0091966 0.007349 0.0097132 0.0082506 0.0084228 0.015373 0.0034295 0.0026201 0.0036402 0.0087158 0.012614 0.0016316 0.011381 0.010051 0.012511 0.017775 0.011802 0.0078388 0.0088022 0.012128 0.0061256 0.0086133 0.0067999 0.0058939 0.0046381 0.0044693 0.0048522 3992.6 0.0031261 0.0068842 0.010049 0.013085 0.012608 0.0092082 0.0073621 0.0097193 0.0082506 0.0084438 0.015384 0.0034295 0.0026153 0.0036496 0.0087158 0.012614 0.0016485 0.01138 0.010051 0.012553 0.01777 0.011777 0.0078367 0.0088082 0.012134 0.0061542 0.0085942 0.0067999 0.0058996 0.0046435 0.0044755 0.0048522 3990.7 0.003149 0.0068842 0.010047 0.013085 0.012612 0.009217 0.0073621 0.0097193 0.0082649 0.0084438 0.015417 0.0034344 0.002612 0.0036496 0.0087158 0.012612 0.0016529 0.01138 0.010051 0.012589 0.01777 0.011777 0.0078344 0.0088175 0.012141 0.0061256 0.0085942 0.0067999 0.0058996 0.0046883 0.0044755 0.0048547 3988.8 0.0031371 0.0068891 0.010046 0.01309 0.012616 0.009225 0.0073927 0.0097343 0.0082706 0.0084543 0.015445 0.0034344 0.0026171 0.0036538 0.0087027 0.012607 0.0016687 0.011378 0.010056 0.012606 0.017797 0.011785 0.0078304 0.0088263 0.012147 0.0061259 0.0085919 0.0068098 0.0059348 0.0047103 0.0044869 0.0048894 3986.9 0.0031404 0.0068916 0.010047 0.013093 0.01262 0.0092359 0.0074187 0.0097409 0.0082755 0.0084739 0.015463 0.00344 0.0026321 0.0036668 0.0086982 0.012606 0.0016367 0.011373 0.010061 0.012621 0.017806 0.011791 0.0078261 0.0088357 0.012161 0.0061266 0.0085918 0.0068175 0.0059713 0.0046883 0.0044891 0.0049132 3984.9 0.0031625 0.0068956 0.010049 0.013097 0.012625 0.0092414 0.0074187 0.0097376 0.0082797 0.0084739 0.01548 0.003466 0.0026345 0.0036884 0.0086951 0.012607 0.0016342 0.011373 0.010067 0.012632 0.017809 0.011791 0.0078261 0.0088458 0.012171 0.0061258 0.0085935 0.0068175 0.0059878 0.004699 0.0044869 0.0049132 3983 0.003149 0.0069 0.01005 0.013097 0.012631 0.0092414 0.0074316 0.0097459 0.0082799 0.0084784 0.015493 0.0034691 0.0026345 0.0036839 0.0086883 0.01261 0.001633 0.011375 0.010067 0.012638 0.017812 0.0118 0.0078261 0.0088576 0.012179 0.006126 0.0085961 0.0068152 0.0060109 0.0047103 0.0044869 0.0049013 3981.1 0.0031404 0.0069047 0.010052 0.01311 0.012638 0.0092543 0.0074527 0.009755 0.0082828 0.0084643 0.015504 0.0034745 0.0026505 0.003693 0.0086932 0.012608 0.0016316 0.011376 0.010062 0.012644 0.017816 0.01181 0.0078207 0.0088576 0.012179 0.0061284 0.0085961 0.0068175 0.0059878 0.0047354 0.0044919 0.0049357 3979.1 0.0031625 0.006909 0.010052 0.01311 0.012649 0.0092543 0.0074614 0.009755 0.0082857 0.008468 0.015515 0.0034897 0.0027131 0.0037029 0.0086944 0.012595 0.0016352 0.011376 0.010069 0.01265 0.017816 0.011822 0.0078176 0.0088695 0.012183 0.006126 0.0085989 0.0068196 0.0059713 0.0047354 0.0044883 0.0049357 3977.2 0.0031445 0.0069117 0.010057 0.013116 0.012657 0.0092543 0.0074527 0.009755 0.0082892 0.0084715 0.015524 0.0034805 0.0026556 0.0036884 0.0086944 0.012602 0.0016402 0.01138 0.010081 0.012656 0.017822 0.011822 0.0078147 0.0088807 0.012183 0.0061284 0.0086066 0.0068196 0.0059713 0.0047354 0.0044883 0.0049013 3975.3 0.0031445 0.0069135 0.01006 0.013118 0.012657 0.0092599 0.0074527 0.0097636 0.0082892 0.0084715 0.015524 0.003466 0.0026303 0.0036762 0.0086932 0.012602 0.0016383 0.011391 0.010081 0.012657 0.017812 0.011818 0.0078113 0.0089084 0.012165 0.0061312 0.0086116 0.0068288 0.0060029 0.0047354 0.0044936 0.0049132 3973.4 0.0031666 0.0069148 0.010063 0.01313 0.012674 0.0092714 0.0074681 0.0097716 0.0082898 0.0084735 0.015536 0.003466 0.0026288 0.0036797 0.0086932 0.012613 0.0016633 0.011397 0.010087 0.012635 0.017818 0.011836 0.0078113 0.0089138 0.012183 0.0061272 0.0086178 0.0068326 0.0060189 0.0047547 0.0044951 0.0049357 3971.4 0.0031415 0.0069178 0.010071 0.013136 0.012691 0.0092783 0.0074735 0.0097872 0.0082893 0.0084773 0.015535 0.0034805 0.0026345 0.0037089 0.0086951 0.012632 0.0015915 0.011397 0.010095 0.012639 0.017818 0.011855 0.0078037 0.0089199 0.012191 0.0061272 0.0086178 0.0068326 0.0060306 0.0047806 0.0044974 0.0049631
% T1.Properties.VariableNames{2:end}
linelbls = T1.Properties.VariableNames(2:end);
L = size(T1,2)-1;
cm = colormap(turbo(L))
cm = 32×3
0.1900 0.0718 0.2322 0.2258 0.1661 0.4566 0.2523 0.2573 0.6429 0.2692 0.3454 0.7912 0.2766 0.4304 0.9014 0.2746 0.5122 0.9736 0.2526 0.5930 0.9987 0.2031 0.6743 0.9708 0.1465 0.7521 0.9067 0.1035 0.8220 0.8240
mrkr = ['o+*.x_|sd^v><ph'];
Lmrkr = numel(mrkr);
hold on
for k = 1:L
mkridx = k;
if k > Lmrkr
mkridx = rem(k,Lmrkr) + 1;
end
plot(T1{:,1},T1{:,k+1}, '-', 'Color',cm(k,:), 'Marker',mrkr(mkridx), 'DisplayName',sprintf('Variable %s',linelbls{k}))
end
hold off
legend('NumColumns',2, 'Location','eastoutside')
This does what you requested, however it might be best to use subplot (or one of its friends, such as tiledlayout or stackedplot), rather than to plot all these together in one plot.
.
  4 Kommentare
Behrad Ze
Behrad Ze am 21 Aug. 2022
Sorry I forgot to do it

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by