Regarding the plot and for loop

1 view (last 30 days)
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.

Accepted Answer

Bob Thompson
Bob Thompson on 19 Jun 2019
Edited: Bob Thompson on 19 Jun 2019
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 Comments
Bob Thompson
Bob Thompson on 19 Jun 2019
Edited: Bob Thompson on 19 Jun 2019
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});

Sign in to comment.

More Answers (0)

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!