Regarding the plot and for loop

1 次查看(过去 30 天)
Hey, I have a one small problem.
This is my sample code: Just focus on only matrix O ;
function my_plotinstat
n = 4; % may be 5, 10, 50, 100 doen't matter now
load('Measuring_Data.mat');
% f = 2000
% T1 is a 2-D matrix of size n*n , assume any numbers
[f,T1] = surendra13_Instat(Measuring_Data);
[Lambda, A, B, D,V, Rho,cP,P, M, A1,O, X, M_1] = Matrix_surendra14;
%Umgebungstemperature = zeros(1,length(f)); % Vektor mit konstanter Umgebungstemeprature
%Umgebungstemperature(1,:)=20;
figure
hold ('on');
for plotnumber = 1:n
if plotnumber == X{1,1}
plot(f/60,T1(plotnumber,:),'Color','g','LineWidth',2,'LineStyle','-');
legend(X{1,2});
elseif plotnumber == X{2,1}
plot(f/60,T1(plotnumber,:),'Color','c','LineWidth',2,'LineStyle','--');
legend(X{2,2});
elseif plotnumber == X{3,1}
plot(f/60,T1(plotnumber,:),'Color','b','LineWidth',2,'LineStyle',':');
legend(X{3,2});
elseif plotnumber == X{4,1}
plot(f/60,T1(plotnumber,:),'Color','m','LineWidth',2,'LineStyle','-.');
legend(X{4,2});
elseif plotnumber == X{5,1}
plot(f/60,T1(plotnumber,:),'Color','g','LineWidth',2,'LineStyle','o');
legend(X{5,2});
elseif plotnumber == X{6,1}
plot(f/60,T1(plotnumber,:),'Color','g','LineWidth',2,'LineStyle','d');
legend(X{6,2});
elseif plotnumber == X{7,1}
plot(f/60,T1(plotnumber,:),'Color','y','LineWidth',2,'LineStyle','s');
legend(X{7,2});
elseif plotnumber == X{8,1}
plot(f/60,T1(plotnumber,:),'Color','y','LineWidth',2,'LineStyle','h');
legend(X{8,2});
elseif plotnumber == X{9,1}
plot(f/60,T1(plotnumber,:),'Color','y','LineWidth',2,'LineStyle','p');
legend(X{9,2});
elseif plotnumber == X{10,1}
plot(f/60,T1(plotnumber,:),'Color','y','LineWidth',2,'LineStyle','>');
legend(X{10,2});
elseif plotnumber == X{11,1}
plot(f/60,T1(plotnumber,:),'Color','y','LineWidth',2,'LineStyle','<');
legend(X{11,2});
elseif plotnumber == X{12,1}
plot(f/60,T1(plotnumber,:),'Color','y','LineWidth',2,'LineStyle','v');
legend(X{12,2});
end
end
hold('on')
plot(f/60,Umgebungstemperature,'Color','b','LineWidth',2);
grid on
box off
end
% xlabel('Zeit in min','FontSize',14);
% ylabel('Temperatur in °C','FontSize',14);
% title('instationäre Knotentemperatur','FontSize',16); % hier im Titel den Knoten xx einfügen oder "der Welle", "des Gehäuse, "der Isolierung", "des RWDR", "des Dichtkontakt", "des Öl" oder "der Luft", je nach Ausgabeparameter
% ylim([0,250]);
% hold('off');
Here X is a cell array, it ask the user to enter which one to be plotted for an example:
X = { 1 , 'Node1';
3 , 'Node3';
4 'Node4'};
Here I am allowing user to plot only max 12 nodes at a time or even less also, depending upon the user.
When I run the code it is showing below error;
Index exceeds matrix dimensions.
Error in my_plotinstat (line 24)
elseif plotnumber == X{4,1}.
Here I know Dimension of the cell array X is not 5 *5 or even more according to code. it is 3*3 now.
But my problem is code should run according to the user input, it doesn't matter whether it is 3*3 or 4*4.
I think here I have to use for loop or if statement to make it correct. But I am not getting what to use or how to use after the first for loop.
Could anyone kndly suggest me an answer?
Thanks in advance.

采纳的回答

Bob Thompson
Bob Thompson 2019-6-19
编辑:Bob Thompson 2019-6-19
The problem is occurring because your X sample is not consecutive values, but you want the loop to look for consecutive values.
When plotnumber == 2, then it is not able to make the match with X ( which contains 1 3 4), and so proceeds along to the next elseif condition. There it is looking for plotnumber == X{4,1}, but this returns the error because X{4,1} does not exist.
  4 个评论
surendra kumar Aralapura mariyappa
@Bob Nbob, Thank you for the suggestion. I didn't think about the other plotnumbers.
But if I use your idea, I can't add legend in the plot.
Kindly see the code.
function my_plotinstat
n = 20;
load('Measuring_Data.mat');
[f,T1] = surendra13_Instat(Measuring_Data);
[Lambda, A, B, D,V, Rho,cP,P, M, A1,O, X,M_1] = Matrix_surendra14;
Umgebungstemperature = zeros(1,length(f)); % Vektor mit konstanter Umgebungstemeprature
Umgebungstemperature(1,:)=20;
figure
hold ('on');
p = cell2mat(X(:,1));
for plotnumber = 1:n
c = find(p == plotnumber, 1);
if ~isempty(c)
c = plotnumber;
if c >= 1 && c <= ceil(n./4)
plot(f/60,T1(c,:),'Color','m','LineWidth',2,'LineStyle','-');
% legend(X{c,2});
elseif c > ceil(n./4) && c <= ceil(n./2)
plot(f/60,T1(c,:),'Color','c','LineWidth',2,'LineStyle',':');
%legend(X{c,2});
elseif c > ceil(c./2) && c <= ceil(3*n./4)
plot(f/60,T1(c,:),'Color','r','LineWidth',2,'LineStyle','d');
% legend(X{c,2});
elseif c > ceil(3*n./4) && c <=n
plot(f/60,T1(c,:),'Color','y','LineWidth',2,'LineStyle','s');
% legend(X{c,2});
end
end
end
hold('on')
plot(f/60,Umgebungstemperature,'Color','b','LineWidth',2);
grid on
box off
end
Here as I said earlier. X cell array is a user input. So user will give both plotnumber(p) and name to that plot. Like below
Now user wants node 4 and 10 should be plotted.
X = { 4, 'Node4'; 10, 'Node10'};
Now only line will be plotted but legend will not be there. so please check my code you can understand better what I am telling.
I need legend also.
is it also possible to give different color or pattern to the line plot? In my code I am considering only 4 color and 4 pattern. is there any way to make at least ten color or pattern for max 10 user input ?
Thanks in advance.
Bob Thompson
Bob Thompson 2019-6-19
编辑:Bob Thompson 2019-6-19
I believe you need to just do legend as all of the names.
legend(X(:,2))
If that sets them up out of order than I would suggest copying them into a specific array, and then inducing them all at once after the loop from that array. Either way it should be happening outside of the loop all at once.
As for increasing the possible size of the input array, I would suggest creating a variable which contains the different possible colors and styles of lines, and then calling that in based on indexing.
fmt = {'b', '-';
'k', '.'};
plot(f/60,T1(c,:),'Color',fmt{c,1},'LineWidth',2,'LineStyle',fmt{c,2});

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by