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

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

Sure -- what portion of the 7x7 do you want to throw away?
But begs the Q? of why not produce the 6x6 instead to begin with?
Hi @dpb I would like to rid of the last row and column of the 7x7 matrix and get 6x6 for further computation. The 7th is the element on which I cause some disturbance and see the effect on reset of the other 6 elements. Hope this explains my objective.
Hi @dpb I would be glad if you can show me how this can me done? Thank you in advance.
I'd be glad to if you'd share the answers to the previous questions so know what it is you actually want for the first -- or, how you get the results to have some idea why they couldn't just as well be the wanted size instead.
We only know what you tell us, which so far is just some general statements, not details at all...
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)

" 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

Hi @dpb I think i did not make myself clear here. That is not my sole purpose here, as mentioned I can do with that but I wanted to get these ouput one by one given out in excel. As you know in Matlab it only gives the value of the last iteration while i need all the iteration values given out in excel format.
Hi @dpb for now lets forget that there is need for matrix reduction. I want all the output from this program to be presented in excel. How would i achieve this? I tried using xlswrite approach but there too it only gives the value of last iteration.
Hi @dpb I added your text at the end of my program but I does not give out the excel file as desired. Am I missing something here. Following are the changes that I made before placing it below my program.
fn=fullfile('C:\Users\Desktop','DataOutput.xlsx');
for i=1:6
Ybus=(Ynn-Ynr*Yrr*Yrn)
writematrix(Ybus(1:4,1:4),fn,"Sheet",1,"Range","WriteMode","append");
end
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.

Products

Asked:

on 28 Jul 2021

Commented:

dpb
on 30 Jul 2021

Community Treasure Hunt

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

Start Hunting!