Write in Excel With Matlab on continues Cells

Hello!, I'm trying to make a code which writes data of a matrix in an Excel Worksheet, however, I'd like to write several matrices one after the other, the problem is that with the function xlswrite I have to specify the cell were the matrix is going to be written, and depends on the data there will be diferents quantities of matrices.
Right now I have this:
fprintf('Select file location \n');
ubic=uigetdir;
name=input('Specify Name of the file: ','s');
comp=strcat(ubic, '\',name, '.xls');
nomhoja='Desplazamiento';
for i=1:np
head={'t (seg)', 'x (cm)'};
xlswrite(comp, head, nomhoja, 'A1');
xlswrite(comp, [rangT',maximox(i,1:num)'], nomhoja, 'A2');
end
As you can see I need to write this matrix [rangT',maximox(i,1:num)'] 'np' times, so when I write 'A1' for the first loop it's fine but for the next one I need it to be 'C1', the next one 'E1'.. etc
Hope You can help me
Thanks!!

 采纳的回答

First put your data in a cell array then write it:
rangT = 1:10;
maximox = rand(10,10);
fprintf('Select file location \n');
ubic=uigetdir;
name=input('Specify Name of the file: ','s');
comp=strcat(ubic, '\',name, '.xls');
nomhoja='Desplazamiento';
C = cell(10,20);
for i=1:10
C(:,(2*i-1):(2*i)) = num2cell([rangT',maximox(i,1:10)']);
end
head={'t (seg)', 'x (cm)'};
Data = [repmat(head,1,10);C];
xlswrite(comp, Data, nomhoja);

6 个评论

Hello!, Thanks for your answer!, I wanted to know something else, where repeating the header in the cells I'd also like to identify each data, this is for example the first to columns corresponds to Story 1, the next two Story 2... until Story n.
So I'll need a loop so it saves the story number, but then how do I write it in excel?
I worked perfectly y shows now something like this:
t (seg) x (cm) t (seg) x (cm)
0 0.400009382 0 7.609245425
0.138632869 1.103930233 0.138632869 7.835078111
0.18036464 2.188400651 0.18036464 8.219013757
0.295613242 2.802956438 0.295613242 9.99247371
And above it I want to put an identifiyer header for example:
Story 1 Story 2
t (seg) x (cm) t (seg) x (cm)
0 0.400009382 0 7.609245425
0.138632869 1.103930233 0.138632869 7.835078111
0.18036464 2.188400651 0.18036464 8.219013757
0.295613242 2.802956438 0.295613242 9.99247371
till sotry n..
Hope you can help me! thanks!
header(1:2:20) = strcat({'Story '},num2str((1:10)'));
header(end+1) = cell(1);
head={'t (seg)', 'x (cm)'};
Data = [header;repmat(head,1,10);C]
it returns me this error In an assignment A(:) = B, the number of elements in A and B must be the same.
this is the code I introduced:
nomhoja='Desplazamiento';
C=cell(c,num);
for i=1:np
C(:,(2*i-1):(2*i)) = num2cell([rangT',maximox(i,1:num)']);
end
header(1:2:num) = strcat({'Story '},num2str((1:np)'));
header(end+1) = cell(1);
head={'t (seg)', 'x (cm)'};
Data = [header;repmat(head,1,num);C];
xlswrite(comp, Data, nomhoja);
np = 10;
num = 8;
rangT = 1:8;
maximox = rand(10,8);
nomhoja='Desplazamiento';
C=cell(num,2*np);
for i=1:np
C(:,(2*i-1):(2*i)) = num2cell([rangT',maximox(i,1:num)']);
end
header(1:2:2*np) = strcat({'Story '},num2str((1:np)'));
header(end+1) = cell(1);
head={'t (seg)', 'x (cm)'};
Data = [header;repmat(head,1,np);C];
xlswrite(comp, Data, nomhoja);

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by