Problem with XLS files read and write

Dear All, I have a single file with a table of 8 column and 200 raw. I would like to create a function that open the XLS file and create 200 XLS files each one with the 8 value of each raw. Can somebody help me please? thanks a lot

回答(1 个)

the cyclist
the cyclist 2011-12-7
Have you tried the xlswrite() and xlsread() functions?
EDIT in response to comment:
You provided no detail on what you have tried, or where you are stuck. Here is the general approach I would take:
-- Use xlsread() to read the origin file into a variable
-- Use a for loop that
  1. extracts each row of that variable
  2. creates a unique filename for each row
  3. writes that row to file using xlswrite()
Maybe you could take that approach, and see if you get stuck anywhere. If you do, then post your code and your specific question about where you are stuck.

12 个评论

I don't know how to move every time one raw lower
ok with txt I done easy because you can use fopen..
Ofid=fopen('totale.csv','r');
OrgFileTxt=textscan(Ofid,'%s','delimiter','\n');
fclose(Ofid);
for k=1:length(OrgFileTxt{1})
NewFName=strcat('fn',num2str(k),'.txt');
NewFid=fopen(NewFName,'w');
fprintf(NewFid,OrgFileTxt{1}{k});
fclose(NewFid);
end
up to xlsread('total.xls','a1:u250')
with xlsread I just read the section that I need but then I don't know how to extract. than of course when I have extracted the file I can use the xlswrites
Are you using this syntax for xlsread: [num,txt,raw]=xlsread(...)?
Then "num","txt", and/or "raw" should be usable in much the same way as you had been using OrgFileTxt, depending on the contents of your file.
I have to admit that I am relying on old memories of xlsread(), because I have not used it in several years. (That command does not work very well on Macs.)
yes with raw that is the complete solution. Now how can I create n files with the structure that I have?
"raw" is a cell array with your data, right? What size is it?
I think you need two steps: (1) Make each individual row variable from the cell array "raw". (2) Write that row to the file.
Which of those two steps are you stuck on? Or maybe it is something else?
[num,var,all]=xlsread('mauri.xls','a1:u250');
for k=1:250
NewFName=strcat('fn',num2str(k),'.xls');
xlswrite{'fn',num2str(k),'.xls',all{1}{k},'a1:u1'}
end
I don't know where is the error
Are you getting a MATLAB error message from that code? If so, what is the message? If not, what do you mean by "error"? Do you get output files, but they don't look as you expect?
??? Cell contents reference from a non-cell array object.
Error in ==> from1toN at 10
xlswrite{'fn',num2str(k),'.xls',all{1}{k},'a1:u1'}
how can I extract all the raw cell? with all{1} I extract only the first value if I would like to extract all the raw?
You could try [all{1,:}] to concatenate all the elements of the first row into one long array. However, if the variables are of mixed type, that won't work. You may have to do more processing on each piece of data, to see the best way to put them all together.
how can I do please?
all(1,:) is the first row of the cell array, so it will be a 1xN cell array. You could try to do xlswrite{'fn',num2str(k),'.xls',all(1,:),'a1:u1'};
I'm not sure if xlswrite() works like that, and I cannot test it here.
If it is acceptable for you to have CSV files instead of XLS files as output, you could use the File Exchange function cellwrite(), available here: http://www.mathworks.com/matlabcentral/fileexchange/7363-cellwrite

请先登录,再进行评论。

提问:

2011-12-7

Community Treasure Hunt

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

Start Hunting!

Translated by