Is there a easy good way to sum up various excel files in matlab?
what i really want is similar to
dos command
type file*.xls> sumfile.xls
I have from 10-100 excel files with similar file name formats excet the date
XXXXX_2010_03_03.xls, XXXXX_2010_03_03.xls and so on.....
Is there a command to copy the files one after other. All files are of diff length so i cannot know the position of the rows after each file. I would like to have them copied in same sheet of excel.
Thanks
Get file names
names=dir('XXXXX-*.xls');
names={names.name};
output='out.xls';
First file. This will overwrite the output each time you run this program - it's up to you if this is the behavior you want.
copyfile(names{1},output);
Cycle through the files
for i=2:length(names)
num_in = xlsread(names{i}); % read the data
num_out = xlsread(output);
range=['A' num2str(size(num_out,1)+1)]; % next free line
xlswrite(output, num_in, 1, range); %always write to the 1st sheet
end
This should work if (1) you only have numerical data and (2) you want to concatenate ("sum", as you put it) the files top to bottom.
If (1) is wrong, please read xlsread's help -- look for txt and raw outputs.
Use xlswrite(filename, M, range) to write your files one after the other. Read the Excel file into M with xlsread.
xlswrite(filename, M, range) writes
matrix M to a rectangular region
specified by range in the first
worksheet of the file filename.
Related
I would like to ask how to use MATLAB to append new columns into existing excel file without altering the original data in the file? In my case I don't know the original number of columns and rows in the file and it is inefficient to open the files one by one and check in practice. Another difficulty is that the new columns may have different number of rows to the existing data so that I cannot use the trick of reading in the data, forming a new matrix and replace the data with the new matrix.
I have seen many posts teaching people how to add new rows but adding new column seems quite a different thing since the columns are named by letters instead of numbers.
Thank you.
You could try reading in the data, use size on the array to determine the number of columns, and then use xlswrite with the range that you want. Have a look here for a function to turn the column number into the excel format: http://au.mathworks.com/matlabcentral/answers/54153-dynamic-ranges-using-xlswrite
Finally I solve it with the following code:
%%%
if (step==1)
xlswrite(filename,array,sheetname,'A1'); %Create the file
else
[~,~,Data]=xlsread(filename,sheetname); %read in all the old data
OriCol=size(Data,2); %get the column number of the old data
NewCol=OriCol+1; %the new array is placed right next to the original data
ColLetter=xlcolumnletter(NewCol);
StartCell=[ColLetter,'1'];
xlswrite(filename,array,sheetname,StartCell);
end
I'm looking to write a script for MATLAB that will import data from a csv file which has a first row containing string headers and the data in each of those columns is either string, date or numeric.
I want to then be able to filter the data in MATLAB according to instances of a particular string and number combination.
Any help appreciated!
Cheers!
I would recommend you to start with reading MATLAB documentation.
[num,txt,raw] = xlsread('myExample.xlsx')
Reads numeric, text and combined data, so, if your data is combined, then you need the cell array raw. After that, you do whatever you want with your cell array (Additional information is not provided since OP did not provide any specific information about the way the data would be filtered)
Try using readtable function in MATLAB.
It correctly imports csv file with header and mixed data type.
xlsread was imported by mixed csv file very incorrectly repeating the some rows while maintaining the same total rows.
I got this after searching for a long time:
MATLAB Central Question/Answer
I am a beginner in netlogo. I find the Netlogo Manual not always as explicit as I feel it should be, as exemplified by the folowing task. My feeling is that this task ought to be relatively simple, but so far I have not been able to accomplish it. I have searched for hints on this forum to help me, but perhaps my problem is so simple that nobody has yet come up with a corresponding question.
The task is: Write data into columns of an Excel file such that for each tick six data points form a row of data across six columns (say across columns A, B, C, D, E, F of the Excel file). I do not find the command to assure that after each data point, having been entered into its column, the next column is selected to enter there the next data point.
This starts already with the headings of the columns for which I give the commands in the setup procedure as below:
...
if (file-exists? "TO_test.csv") [carefully [file-delete "TO_test.csv"] [print error-message]]
file-open "TO_test.csv"
file-type "number,"
file-type "name,"
file-type "age,"
file-type "height,"
file-type "income,"
file-type "status,"
file-close
....
The output in the Excel file is then
number, name,age,height,income,status,
all in one comlumn. I use the 'type' command because that assures that entries are made in the same line. I added the ‘,’ to each string because I believe to have picked up somewhere that this causes the shift to the next column (which, however, it does not). If I use 'file-print' instead of 'file-type' I get an entry in successive lines instead of columns, because here the ‘,’ causes a ‘return’ command.
What I would like to get is the following (where the first line shows the given headings of the Excel file):
A B C D E F
number name age height income status
I have an excel file, which is a mix of text file and numerical values.
For instance, the file look like this,
25 file1
26 file2
Here the 25 is an numerical value in the first cell (row 1, column1). "file1" represents the content in the second cell(row1, column2). It can be short text file composed of multiple paragraphs.
I want to load this excel file into matlab, and store it into a 2*2 matrix. Each matrix entry corresponds to a matrix cell.
I tried xlsread, but it did not work. I also tried textscan, but it seems to be able to handle the scenario where a cell has a string only. Here, the contents of some cells are text files itself.
If you are reading an Excel file using XLSREAD, you can use the third output argument to retrieve the both the textual and numeric data (unprocessed).
Example:
>> [~,~,raw] = xlsread('Book1.xls')
raw =
[25] 'hello world.'
[26] [1x38 char]
>> raw{2,2}
ans =
this is an example
of multi-line
text
Note that XLSREAD is limited to the capabilities of MS Excel to open/read files, so some especially large files (in my experience 1 million+ rows) will get only partially read.
I have several lists in Matlab that I want to write to the same excel file.
I have one list xordered and another list aspl. I do not know the length of the lists till after I run the Matlab program.
I used
data = xlswrite('edgar.xls',fliplr(sortedx'),'A2:A3000')
for the first list but when I tried to write another list to the same file like this I ended up with two different excel files named edgar.xls
asp_data = xlswrite('edagr.xls', fliplr(aspl'), 'B2:B3000')
Is there a way I can write both of these lists into the same excel file? xordered in the A column and aspl in the B column?
d = [ fliplr(sortedaspl'), fliplr(sortedx')];
Makes a matrix from A2 to B300
data = xlswrite('edgar.xls',d,'A2:B300');