MATLAB Answers

Export iterative matlab program output in matrix to excel with a reduction in matrix size.

2 views (last 30 days)
Kinga Gyeltshen
Kinga Gyeltshen on 28 Jul 2021
Commented: dpb on 30 Jul 2021
I have a Matlab program that outputs 7x7 matrix for 40 times. I need to export every output from the above iteration and obtain a 6x6 matrix in excel. Is it possible?
  5 Comments
Kinga Gyeltshen
Kinga Gyeltshen on 30 Jul 2021
Hi @dpb here is the program for which I want the output from every iteration in excel.
linedatas =[1 4 0 0.1184 0 -8.4459
2 7 0 0.1823 0 -5.4855
3 9 0 0.2399 0 -4.1684
4 5 0.0100 0.0850 1.3652 -11.6041
4 6 0.0170 0.0920 1.9422 -10.5107
5 7 0.0320 0.1610 1.1876 -5.9751
6 9 0.0390 0.1700 1.2820 -5.5882
7 8 0.0085 0.0720 1.6171 -13.6980
8 9 0.0119 0.1008 1.1551 -9.7843];
linedata = linedatas;
fb = linedata(:,1);
tb = linedata(:,2);
r = linedata(:,3);
x = linedata(:,4);
c = linedata(:,5);
b = linedata(:,6);
z = r + 1i*x;
y = 1./z;
w = c+1i*b;
nbus = max(max(fb),max(tb));
nbranch = length(fb);
Y = zeros(nbus,nbus);
for k=1:nbranch
Y(fb(k),tb(k))=-y(k);
Y(tb(k),fb(k))=Y(fb(k),tb(k));
end
%%% Formation of the Diagonal Elements...
for n=1:nbus
for k=1:nbranch
if fb(k)==n
Y(n,n)=Y(n,n)+y(k);
elseif tb(k)==n
Y(n,n)=Y(n,n)+y(k);
else
end
end
end
Y;
B=[0;0;0;0.1670i;1.2610-0.2634i;0.8777-0.0346i;0.2275i;0.9690-0.1601i;0.2835i];
B=diag(B);
Y1bus=Y+B;
columnold=Y1bus(:,3);
rowold=Y1bus(3,:);
format
for j=3:9
Y1bus=Y+B;
Y1bus(:,3) = Y1bus(:,j);
Y1bus(:,j)= columnold;
Y1bus;
newrow=Y1bus(3,:);
Y1bus(3,:) = Y1bus(j,:);
Y1bus(j,:)=newrow;
Y1bus;
Ynn=Y1bus(1:3, 1:3);
Ynr=Y1bus(1:3, 4:9);
Yrn=Y1bus(4:9, 1:3);
Yrr1=Y1bus(4:9, 4:9);
Yrr=inv(Yrr1);
Ybus=(Ynn-Ynr*Yrr*Yrn)
end
From this I want a 3x3 matrix after each iteration.

Sign in to comment.

Answers (1)

dpb
dpb on 30 Jul 2021
Edited: dpb on 30 Jul 2021
" I would like to rid of the last row and column of the 7x7 matrix and get 6x6 for further computation. "
That would simply be if you have matrix M
M=M(1:6,1:6);
Sorry, I got interrupted and had to leave for a while...figured might as well send what had already typed..
Use writetable or writematrix instead depending upon the form -- if as above, you just have a matrix M, then writematrix is the suitable function.
Then you have to decide how you want to save the data -- then whether you want the data with/without header(s), on one or sequential sheets in the workbook, etc., etc., etc., ...
To continue to write at the end of the existing data, use the 'WriteMode', 'append'
fn=fullfile('YourRootDirectory','PickaFileNameOutput.xlsx'); % build a filename
for i=1:numberIterations
M=...calculate here...
writematrix(M(1:nToKeep,1:nToKeep),fn,"Sheet",1,"Range","WriteMode","append");
end
You can fix to use 'overwrite' (the default if don't specify at all) the first pass through the loop with an "if...else...end" construct to begin again if the file already has data in it and want to start fresh.
All up to you to decide what you want/need...
  4 Comments
dpb
dpb on 30 Jul 2021
Ybus=(Ynn-Ynr*Yrr*Yrn)
Unrecognized function or variable 'Ynn'.
The above expression is invariant inside the loop so you'll get the same thing everytime...you have to put this somewhere in your code in which Ybus is being modified for it to have any effect.
PS: Ignore the red text; that's a fignewton of the new, fancy editor in the forum, apparently, that tried to run the code snippet when I pasted it from the above after I converted your text to code format -- and it won't let me delete it. Sometimes there's just too much "help".

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!