I have a large number of measurement data in excel files. I need to read and process this data using matlab. The problem I have is that not all excel files contain the same amount of data.
I have tried the following
excel = actxserver('excel.application');
xlswb = excel.Workbook.Open(myFile);
data = xlswb.Sheets.Item(mySheet).get('Range', 'A1', 'L10000').Value;
This "works" as there will not be more than 10000 rows and 8 columns (in my case). However, this is very dirty code and I have to check for each file where the data actually ends.
Is there a way to read a whole sheet (similar to the xlsread function)?
Thanks!
Sheets("SheetName").UsedRange will get you a collection every used cell in that sheet. However, if cell L10000 had data and it was cleared, it will still make part of that collection.
Related
I have slightly different data but same format in one excel sheet.
Each data set is 5 columns. The first data set is column A-E, the second data set is column F-J, all the way through to DID-DIH
What I would like to do is to extract these to either their individual sheets or individual workbooks
Is this possible? Perhaps using VBA code?
Sorry I am an amateur trying analyse a massive data set
A good way to get started would be to hit the record macro button and copy the first columns manually. Then stop the recording and look in the VBA editor at the code produced. Wrap this in a loop and make the necessary changes to move columns etc. Have a go and post the code if you get stuck.
I have an Excel spreadsheet that generates CSV scripts used in an application. The scripts must be in a very specific format, and I save a master in XLSX format with protected sheets and data validation to save the CSVs from rather than directly edit the CSVs, as directly editing the CSVs can lead to mistakes.
The issue is that the scripts can be of nearly any length. The left column of each line can only be one of a certain set of values, and the last line has to say "END". The only way I can do this without VBA is the following formula in the A column, from row 7 (the first 6 are header information) to row 1048576 (last Excel row) and protect the sheet with column A locked:
=IF(AND(ISBLANK(B368),NOT(ISBLANK(B367))),"END",IF(ISBLANK(B368),"",A367))
This makes the last row say "END" in column A, and all rows after blank, which is what is desired. The problem is that now when the CSV file is saved, it will always have 1048576 rows, with all the bottom rows containing the delimiters ",,,," . This won't work, the CSV file needs to stop after the "END" row. Is there a way to write the formula that will cause Excel to ignore the cells which evaluate to blank when saving to CSV or an alternate way to save to CSV in Excel that will ignore all the rows that evaluate to blank?
Note: I have a solution in VBA already that I can use on my own machine (it copies the data up to "END", pastes in a new sheet in text only format, then saves as CSV with the name of the original worksheet). I want to share this sheet, however, and getting around the security constraints to share macros at my company is a pain. So I'm looking for a way this might be done without Macros, if it's possible at all.
In looking for an answer I found this link, which is similar, but not the same:
Saving Excel data as csv with VBA - removing blank rows at end of file to save
As the "blanks" I have are active rows because they contain formulas, this method will not work.
Manually deleting the rows / columns will work to reset the size, as GSerg noted in the other question. Alternatively, also as suggested by GSserg, you can copy the data to a new sheet before saving.
Otherwise, an easy fix might be to create a small post-excel / pre-processing script - perhaps using a batch file - Batch / Find And Edit Lines in TXT file - or a similar solution in any small scripting language to remove the extra rows.
I have a large data set in SPSS (v20) with null values for some observations.
I tried saving as an excel 2007 file, but when I open excel file "#NULL!" appears in cells where values are null. I'm going out of ram when trying to use 'find and replace' function in Excel.
T tried saving as a csv file then I got a space in the cells where values are null.
Could anybody advice on this please?
I typically save as CSV and then in excel save as .xlsx. All missing values are then, as you noted, allocated a space which I accept as representing sysmis values.
When I work with a file that has been saved directly to Excel, (ie many "#NULL" values), I use a VBA macros which does a find/replace row by row. The macro is quicker than doing it all at once as this typically starts to slow to infuriating speeds. The macro is still not as fast as one would want...which is why I go via CSV.
According to the command syntax reference, #NULL! values occur only for system missing values. So to prevent that you need to assign the system missings a value - for that you can use the RECODE command (e.g. RECODE MyVar (SYSMIS = -9)(ELSE = COPY). would work for a number field in which -9 can not be a valid value).
Depending on what you want the value to be when written to the sheet, you can then use the /CELLS=VALUES subcommand on SAVE TRANSLATE to save the assigned missing numeric category (IMO a bad idea for spreadsheets) or you can assign the missing value a VALUE LABELS and use /CELLS=LABELS to save the string label in the cell.
I have a spreadsheet where I can enter a reference number and Excel generates a table based on the data tied to that reference number.
Rather than enter each reference number individually and copy the resulting table manually, is it possible to automatically iterate the process of entering the reference numbers (from an existing list) and exporting the results separately?
My Idea:
I didn't quite understand HOW you need to automatically enter those reference, but a good idea would be writing all the numbers you need in a .txt file, then program a button which loads a msoFileDialogFilePicker to select your file. When the file is selected, you can open it with a New FileSystemObject, read it with ReadLine and filling an array with all the numbers you have written in the .txt file. Then with a forcycle from LBoundto UBoundof that array you can enter those numbers automatically in your Range.
To export everytime your results table (which again I didn't quite understand HOW you want it to be exported), an idea would be a Worksheet Changeevent (if the results table appear in your spreadsheet) in which you can write down some code to copy-paste the results table in a new worksheet and then save it, or make a .pdf of the results table from its area.
The possibilities are really too many to write a single piece of code to show you :)
I have example.xlsx file that contains some data.
I want to show the data in a GUI and I want there is an option to edit the xlsx file in the GUI.
Is there an option to do this? Does someone know anything about it?
I tried to find the xls via the ActiveX control, but I didn't see an option for that :/
I have matlab 2010a and these are my options when I press 'ActiveX control':
thank you :]
You don't need activeX, matlab has the built-in functions, xlswrite and xlsread:
Help File:
xlswrite(filename,A) writes array A to the first worksheet in Excel
file filename, starting at cell A1.
xlswrite(filename,A,sheet) writes to the specified worksheet.
xlswrite(filename,A,range) writes to the rectangular region specified
by range in the first worksheet of the file. Specify range using the
syntax 'C1:C2', where C1 and C2 are two opposing corners that define
the region.
xlswrite(filename,A,sheet,range) writes to the specified sheet and
range.
status = xlswrite(filename,A,sheet,range) returns the completion
status of the write operation: true (logical 1) for success, false
(logical 0) for failure. Inputs sheet and range are optional.
[status,msg] = xlswrite(filename,A,sheet,range) returns any warning or
error message generated by the write operation in structure message.
Inputs sheet and range are optional.
and:
[num,txt,raw] = xlsread(filename) reads data from the first worksheet
in the Microsoft Excel spreadsheet file named filename and returns the
numeric data in array num. Optionally, returns the text fields in cell
array txt, and the unprocessed data (numbers and text) in cell array
raw. If your system does not have Excel for Windows, xlsread operates
in basic import mode, and reads only XLS or XLSX files.
[num,txt,raw] = xlsread(filename,sheet) reads the specified worksheet.
[num,txt,raw] = xlsread(filename,range) reads data from the specified
range of the first worksheet in the file. Specify range using the
syntax 'C1:C2', where C1 and C2 are two opposing corners that define
the region.
[num,txt,raw] = xlsread(filename,sheet,range) reads from the specified
sheet and range.
[num,txt,raw] = xlsread(filename,-1) opens an Excel window to
interactively select data. Supported only on Windows systems with
Excel software.
[num,txt,raw] = xlsread(filename,sheet,range,'basic') reads data from
the spreadsheet in basic mode, the default on systems without Excel
for Windows.
[num,txt,raw,custom] = xlsread(filename,sheet,range,'',functionHandle)
reads from the spreadsheet, executes the function associated with
functionHandle on the data, and returns the final results. Optionally,
returns additional custom output, which is the second output from the
function. xlsread does not change the data stored in the spreadsheet.
Supported only on Windows systems with Excel software.
http://www.mathworks.com/help/techdoc/ref/xlswrite.html
With your choice of user interaction and display style (not sure if you want plots or tables, buttons or sliders) this allows you to interact with xls documents.
Try activeX controls in GUIDE. One of the is Excel control.