My Excel writing program does not stop

I have two arrays of vextors V1 and V2. Array V1 has 100 row vectos and array V2 has 100 row vectors. i,e, each vector is a row vector and is of 4 elements, i.e.
V1=[1 2 3 4]
[2 1 4 1]
[15 8 0]
............
upto 100
likewise
V2=[1 5 7 9]
[1.2 3.4 5.1 6.1]
[2.4 6.1 7.1 9.2]
.......................
up to 100
I want to write vectors of array V1 in column A and vectors of array2 in column D of excel file.I have written the following program:
tt=1:100;
nn=0;
for n=1:100
nn=nn+1;
filename='myfile.xlsx';
strV1=num2str(V1);
for jj=1:size(strV1,1)
xlswrite(filename, cellstr(strV1(jj,:)), 'Sheet1', ['A',num2str(jj)]);
end
strV2=num2str(V2);
for ll=1:size(strV2,1)
xlswrite(filename, cellstr(strV2(ll,:)), 'Sheet1', ['D',num2str(ll)]);
end
end
The above program works well, but it does not stop by itself. it continues to run even if you allow it to run for several days. I want that when the task is finished, the program stops automatically because often I have to press CTRL+C several times, only then it stops and when I check the excel file, it has written those vectors already but still it continues to run. What is the problem with this program?

3 comentarios

Why can't you just write all values with one command?
V = 1:10;
xlswrite('data.xls',V','sheet1','B1')
Sadiq Akbar
Sadiq Akbar el 22 de Feb. de 2020
As I told you I have array of vecttors, i.e. each element of the array V is a row vector. I want to write one row vector in A1 cell of Excle file. 2nd row vector in A2 cell of Excel file, 3rd row vector in A3 of Excel file and so on. I don't want to make each vector as column vector and write them in separate columns. But rather I want to write each row vector of the array in one cell, then 2nd row vector in 2nd cell of the same column, then 3rd row vector in 3rd cell of the same column and so on.
Guillaume
Guillaume el 1 de Mzo. de 2020
"I want to write one row vector in A1 cell of Excle file"
While that's easy to do (without any loop as well), why on earth would you want to do that? Indeed, the numbers would have to be changed to text in excel, so they would be completely unusable with any formula. It doesn't sound like you'll be using excel the way it's meant to be used so there's probably a better way of achieving your end goal. What is your end goal?

Iniciar sesión para comentar.

 Respuesta aceptada

darova
darova el 23 de Feb. de 2020
Editada: darova el 23 de Feb. de 2020
It this a success?
V1 = randi(100,5,5);
for i = 1:size(V1,1)
vv1 = { num2str(V1(i,:)) };
xlswrite('data.xls',vv1,'sheet1',['B' num2str(i+1)])
end
I gave an idea from here

13 comentarios

Sadiq Akbar
Sadiq Akbar el 27 de Feb. de 2020
Thank you very much for your gauidance. But still there is a problem. In my question your reply was:
Why can't you just write all values with one command?
V = 1:10;
xlswrite('data.xls',V','sheet1','B1')
Then after my comments, your answer is:
It this a success?
V1 = randi(100,5,5);
for i = 1:size(V1,1)
vv1 = { num2str(V1(i,:)) };
xlswrite('data.xls',vv1,'sheet1',['B' num2str(i+1)])
end
I gave an idea from here
What did I do after this:
In my data I have some column vectors of size 100x1 and some row vectors of size 100x4. I applied your 1st logic to my column vectors and your 2nd logic to my row vectors. Both your logic works, but there is a little problem which is that the row vectors are written in column A in which the 1st row is empty. This is very good. I liked it becuase later I can manually give a label Estimated Vector to column A manually.
But there is no empty space for label in column B becuase later on I will put my label "time" there manually. I send you a sample what I did according to you.
%----------------------------------sample code starts here----------------------------------
tt=1:10;%--------------(1)
nn=0;
for n=1:10 %------------(2)
nn=nn+1;
%-------------For writing estimated vectors to column A------
for jj = 1:size(two,1)
strtwo = { num2str(two(jj,:)) };
xlswrite('data.xls',strtwo,'sheet1',['A' num2str(jj+1)])
end
%------------For Writing time to column B---------------------
time=time1';
xlswrite('data.xls',time,'sheet1','B1')
end
%----------------sample code ends here--------------------------------------------
The data file that I got is in the attachement.
Now since I found a hope that you can solve my current problem by the grace of Allah, so let me do a last request (and I am extremely sorry for putting burden on you).
I want that the 1st row in each column must have labels written and then below lable is the data. For this purpose I have attached my desired file.
Thank you once again for your kind replies.
Sadiq Akbar
Sadiq Akbar el 27 de Feb. de 2020
One thing more. My program worked well but the problem with that was that it didn't stop though the task would have been completed, i.e. the whole data would have been written to the excel file but the program didn't stop by itself, i.e. it showed busy all the time.
When I converted my program accrding to yours instructions, still it has the same problem, i.e. the task is complete but it shows busy status. So the problem is still there.
I hope this will make you happy
header = {'Estimated Vector' 'time' 'pressure' 'Temperature (in Kelvin)'};
xlswrite('data.xls',header,'Sheet1','A1:D1')
Sadiq Akbar
Sadiq Akbar el 1 de Mzo. de 2020
Thank you very much for your kind replies, guiadance and precious time. I am sending you a small portion of my program that I have chnaged according to yours instructions which is:
for n=1:5 %--------main-loop---------
header = {'Estimated Vector' 'Absolute Error'};
filename='myfile.xlsx';
xlswrite(filename,header,'Sheet1','A1:B1')
%-------------For writing estimated vectors to column A------
% filename='myfile.xlsx';
for jj = 1:size(two,1)%for jj=1:size(strtwo,1)
strtwo = { num2str(two(jj,:)) }; %xlswrite(filename, cellstr(strtwo(jj,:)), 'Sheet1', ['A',num2str(jj)]);
xlswrite(filename,strtwo,'sheet1',['A2' num2str(jj+1)])
end
%-------------For writing Absolute Error to column D----------------------
for jj = 1:size(Error,1)
strError = { num2str(Error(jj,:)) };
xlswrite(filename,strError,'sheet1',['B2' num2str(jj+1)])
end
end
When I executed this, I got the file which I have attched in the attachement. As you can see, the labels are ok, but the entry of data starts from row 22 in both the columns and 8 rows are written instead of 5 rows and when these 8 rows are written then it starts writing from row 210 and writes 80 rows till 299 and when these 80 rows are complete, then it strat writing from row 2100 and writes 2 rows. So it means its not working.
Please use special buttons for code inserting
Try to change your loops as following
%-------------For writing estimated vectors to column A------
% filename='myfile.xlsx';
k = 0;
for jj = 1:size(two,1)%for jj=1:size(strtwo,1)
k = k + 1;
strtwo = { num2str(two(jj,:)) }; %xlswrite(filename, cellstr(strtwo(jj,:)), 'Sheet1', ['A',num2str(jj)]);
xlswrite(filename,strtwo,'sheet1',['A' num2str(k+1)])
end
%-------------For writing Absolute Error to column D----------------------
k = 0;
for jj = 1:size(Error,1)
k = k + 1;
strError = { num2str(Error(jj,:)) };
xlswrite(filename,strError,'sheet1',['B' num2str(k+1)])
end
Why do you need external loop for?
for n=1:5 %--------main-loop---------
Sadiq Akbar
Sadiq Akbar el 2 de Mzo. de 2020
Thanks for your prompt response. I really appreciate your programming skills. You helped me alot. When I changed my program according to yours instructions, not onlly it worked now but also the permanent problem of busy status is gone now. Thanks again from the core and kernel of my heart dear darova.
Regards,
darova
darova el 2 de Mzo. de 2020
you are welcome
Sorry to distureb you again. As I told you that it works now. Yes it works but when I write the following code, it works:
filename='myfile1.xlsx';
header = {'Estimated Vector' 'Temperture' 'Absolute Error'};
xlswrite(filename,header,'Sheet1','A1:C1');
%-------------For writing Estimated vectors to column A------
k = 0;
for jj = 1:size(two,1)
k = k + 1;
strtwo = { num2str(two(jj,:)) };
xlswrite(filename,strtwo,'sheet1',['A' num2str(k+1)])
end
%-------------For writing Absolute Error to column B----------------------
k = 0;
for jj = 1:size(Error,1)
k = k + 1;
strError = { num2str(Error(jj,:)) };
xlswrite(filename,strError,'sheet1',['B' num2str(k+1)])
end
it works i.e. the top cells of columns A and B are given labels and then data is written below that. But when I increase one more cloumn of excel file, then it doesn't work.
filename='myfile1.xlsx';
header = {'Estimated Vector' 'Temperture' 'Absolute Error'};
xlswrite(filename,header,'Sheet1','A1:C1');
%-------------For writing Estimated vectors to column A------
k = 0;
for jj = 1:size(two,1)
k = k + 1;
strtwo = { num2str(two(jj,:)) };
xlswrite(filename,strtwo,'sheet1',['A' num2str(k+1)])
end
%-------------For writing Absolute Error to column B----------------------
k = 0;
for jj = 1:size(Error,1)
k = k + 1;
strError = { num2str(Error(jj,:)) };
xlswrite(filename,strError,'sheet1',['B' num2str(k+1)])
end
%------------For Writing Temperature to column C---------------------
temperature=one';
xlswrite(filename,temperature,'sheet1','C1')
the excel file that I get when I execute the 2nd code is attached herewith. As you can see there is no label with the column C. what should I do now?
darova
darova el 2 de Mzo. de 2020
Sadiq Akbar
Sadiq Akbar el 3 de Mzo. de 2020
Thank you very much darova. Indeed you helped me lot. Now it works. I have no words to thank you. But I thank you from the core and kernel of my heart. Indeed you are a good programmer. But how can I become expert like you? Would you like to give me some useful tip?
Thank you once again.
Regards
darova
darova el 3 de Mzo. de 2020
  • But how can I become expert like you?
practice, practice ...
Stephen23
Stephen23 el 3 de Mzo. de 2020
"But how can I become expert like you?"
reading documentation, reading documentation...
Sadiq Akbar
Sadiq Akbar el 4 de Mzo. de 2020
Thank you very much darova and Stephen Cobeldick for your useful suggestions. Indeed both of you are right. I will try my best in sha Allah.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 21 de Feb. de 2020

Comentada:

el 4 de Mzo. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by