How to export an array text which created by a string and numbers to excel?

1 visualización (últimos 30 días)
Hi,
I have a numeric array a=0:100:1000
I want to precede each element of the array with 'WL' and I will get a new array: 'WL0', 'WL100', 'WL200',..., 'WL1000'.
Then I will export it excel by xlswrite. Could someone please help me to do it? I wrote by myself but I couldn't get the result I desired.
Here is my code:
clc
clear all
format short
a=0:100:1000;
wl={};
for i=1:length(a)
text=sprintf('WL%d',a(i));
wl={wl;text};
end
xlswrite(filename,wl,1,'A6');
Thanks.

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 4 de Oct. de 2014
Khanh - you almost have it. w1 has been (correctly) defined as a cell array, it is just the vertical concatenation that is failing at the line
wl={wl;text};
which seems to result in a 2x1 object where the first is a cell array, and the last is the last value added (text). Just change this line to the following which will create the column that you desire
wl=[wl;text];
and assign a filename
filename = 'myExcel.xls'
and you should be good to try again.
  1 comentario
Khanh
Khanh el 4 de Oct. de 2014
While waiting the answer, I wrote the 2nd code to do it. But it consumed more time than the 1st one with your help. Thank you so much.
My 2nd code:
for i=1:length(a)
text={sprintf('WL%d',a(i))};
range=sprintf('a%d',5+i);
xlswrite(filename,text,1,range);
end

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by