How can I import a specific sheet in an Excel file into Matlab (as an array or table)?
Apparently xlsread is not recommended in the official documentation. However, their recommended method of readtable does not allow you specify a sheet name (I think? perhaps missed it?)
Using test = xlsread('myfile.xlsx', 'my sheet name') seems to work fine in my case, except it skips column headers. Is there a way to keep headers?
Using test = readtable('myfile.xlsx') keeps the headers but just automatically imports the first sheet.
On Windows, Matlab R2018a.
By default, readtable reads the first sheet. You can specify sheet number/name as well to read your desired sheet.
test = readtable('myfile.xlsx','Sheet','my sheet name');
Please read the documentation for more details.
Related
I want to use xlsread in MATLAB R2017b to read from an externally supplied data file. Usually, this works fine for me. However, in this case I get data I can't find in the .xls file and I don't know what happened.
Here is screenshot of the .xls:
and here of the corresponding raw from xlsread:
Note that there is data in MATLAB (e.g. 'Report tem...') that cannot be found in Excel, that the columns are in a different order and that their headers also differ.
The data file is from Svenska Kraftnät, the Swedish Transmission System Operator and contains the generation and consumption of electrical energy for a certain year. You can find it here.
I use the following line to import the data in question (I am only interested in the numerical data and the timestamps, but used the raw to try to understand what is going on here):
[num,~,raw] = xlsread('n_fot2013-01-12.xls');
I am sorry if this a bad format for the question or if this is a dupe, but I didn't have a clue how to make this question more general. Please feel free to suggest improvements!
Your workbook has a hidden sheet in it, and it is that sheet that is being read.
To read the visible sheet, specify the sheet name:
[num,~,raw] = xlsread('n_fot2013-01-12.xls','Förb + prod i Sverige');
To view the the hidden sheet, on the Home tab, in the Cells group, click Format > Visibility > Hide & Unhide > Unhide Sheet. Then select the hidden sheet.
There isn't a way to tell xlsread to only read visible sheets, and by default it reads the first sheet (hidden or not).
I've been saving Google Sheets to Excel without any problems for a while. These sheets have always successfully saved and opened in Excel with the importrange function. However, recently it hasn't been successfully saving correctly.
It used to just have the static value (e.g, 40). There used to be an IFERROR in the first cell in the header row but now it exists in every single cell.
E.g, each cell would have something like this:
=IFERROR(__xludf.DUMMYFUNCTION(importrange(blahblah)),"40").
DUMMYFUNCTION throws an error and "40" is returned as a result. but "40" is a string, not an integer which messes up all my formulas.
I also know this isn't an Excel issue because OpenOffice is doing the same thing with the file.
I'm pretty sure this would be a bug because why would it be working for months and then suddenly stop working?
What should I do?
I'm thinking it's a bug too.
Workarounds
On Excel
Copy and paste as values only the ranges with IFERROR(__xludf.DUMMYFUNCTION(..., then use Excel's UI tools to convert numbers shown as text to numbers.
Selectively remove quotes on the IFERROR second argument of the cells causing problems
Remove =IFERROR(__xludf.DUMMYFUNCTION(),"value") except value (we could use Excel's built-in FIND & REPLACE for this)
On Google Sheets
Use Copy > Paste as values only on the range areas having formulas with non-compatible functions like IMPORTRANGE, QUERY, FILTER, etc.
If you only need the values, download it as CSV instead of XLSX
IMPORTANT
In order to help to prioritize this issue, send feedback to Google. To do this open a Google Sheets spreadsheet, click on Help > Report a problem, then fill the feedback form and submit it.
Related stuff
I posted 5 small articles about this in Spanish. You could find them listed on https://www.rubenrivera.mx/p/descargar-hcg-excel.html.
We accidentally created a workaround for this bug with a different sheet that was just set up like this.
This works when you IMPORTRANGE into another Google Sheet. We are doing it into a Google Sheet with a single worksheet - haven't tried it with multiple.
It's going to sound a little nuts but it works for us.
In the first cell of your import range put a hyperlink in the original document you are importing from. This is in the first cell of the import range. We linked it to a worksheet in the original document. It has worked and failed with an external link. With an external link it worked when I linked it to an internal link, then changed it. But when I deleted the cell and just straight linked it to an external URL it didn't work.
Then #timbo was right - put data validation in. This can be in part of the document that isn't being imported into the second sheet. I put it in the first line of the import range but outside what I was importing. It might have to be the first line. I just put a date in one cell, then in the next cell data > data validation > then choose that one date as the data range.
For aesthetics I have hidden the first row in one Google Sheet I am importing into. In another I made the first cell link the title of the sheet and put the data validation outside the import range. Both of these work.
Let me know if this works for you.
Until this bug is fixed, a workaround is to put a data validation (Data > Data Validation) on the imported data (Any kind of data validation will do).
Does anyone know how to solve my problem?
Im able to import a Excel File into my Matlab GUI, the problem is the file exist in a few sheets combined together (example: monthly data). I'm trying to import the data from different sheets on the excel file but was not able to (anything from the 2nd sheet i can't import). Anyone knows of a code to import the data?
I guess you used xlsread in order to read data from an XLS file. If you do not add arguments to the function, it will only read the first worksheet. Please check the xlsread documentation by typing doc xlsread in your matlab session.
You can see that you can specify the worksheet with the following code :
num = xlsread(filename,sheet)
I'm using the following Code to import a raw data extract (mysql; output format *.xlsx) from an excel sheet (source) into a sheet (target).
Importing Excel spreadsheet data into another Excel spreadsheet containing VBA
The target sheet is a excel-template contains some macros e.g. to allow users to easily sort data.
All is working very well, except that some data are malformed after import. Its really strange but due to any reason target is not an exact copy of the source sheet. Issues I've for example with date format and decimals.
I've tried several things to tweak the code but failed.
I believe that the way how the import in target sheet is handled by code is the reason why it doesnt work.
Now my question is if there exits any other way to import data from excel to excel.
Also I'm looking for a solution how I could solve the problem that excel formats all values automatically as text?
Any help would be highly appreciated.
Kind regards
Try replacing this line
targetSheet.Range("A1", "C10").Value = sourceSheet.Range("A1", "C10").Value
With this
sourceSheet.Range("A1", "C10").Copy
targetSheet.Range("A1", "C10").PasteSpecial
The former only copies the values. The latter copies both values AND its formatting, so things like date formats and number of decimals should be included as well.
thanks for looking at this problem, I hope I can get some help, as I am not very experienced with VBA syntax in excel.
Background:
I will be receiving a large (1000's of lines) CSV file that will contain data entries of various lengths. Each line will begin with a code (eg, 01, 02,..., 50) and have a series of data entries following it based on that code.
So, for example
01,data,data,data
01,data,data,data
02,data,data,data,data
etc...
I need to import all of this data into an existing excel workbook that already has separate tabs and headers created to correspond with the data type.
What I believe needs to be done, is to import the csv to a new, blank sheet, then run a vba program to check the data code, and move the line to the corresponding tab. I would also like to preserve the formatting on the destination sheet.
Ultimately, what I think I need is a VBA program to read the code cell, and move the line to an existing tab based on that code, and loop through the whole column.
Most of the existing solutions I have found involve the creation of new tabs, but I wish to parse the raw data into existing tabs with headers and formatting. I am aware this may require me to manually type in the code and destination tab names in the program's logic - That will not be an issue as long as I have a base to start with!
Thanks again for your help, and let me know if I can provide any more information.