programmatically determine the "variablenamesline" value for import options.
3 views (last 30 days)
Show older comments
Calvin Harrington
on 5 Apr 2023
Commented: Calvin Harrington
on 6 Apr 2023
My situation:
I have a piece of equipment from which the user can export data in csv format. The top rows of the file contain values for various settings. Below that section are columns of data at the top of which is a header row. The default header values for each column are always the same. I want to import the data in the columns.
My problem:
Depending on the instrument settings when the files were exported there are a different number of rows of settings values. This means that the location of the data header row can be different from file to file. I have attached images of two different files. In one the data header row is row 40 (Capture.JPF) and the other it is row 41 (Capture2.JPG).
My question:
How can I find the row that contains the correct header value (the one with my variable names) and then set the variablenamesline property accordingly so that I always import my data regardless of how the user had the unit configured at the time of exporting the data? Can I use detectImportOptions() and readTable() to do so? That is what I have been trying. Is there a better way?
Note: I am certain that this question has already been answered somewhere. I'm sorry if I'm repeating it. However I have already put in about two and a half hours searching mathworks, the internet in general and the documentation for an answer and so now am turning to you all directly. Thanks again for your help.
Details:
My Matlab version is
MATLAB Version: 9.10.0.1710957 (R2021a) Update 4
MATLAB License Number: XXXXXXXX
Operating System: Microsoft Windows 10 Pro Version 10.0 (Build 19045)
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
Thanks in advance for your help.
3 Comments
Walter Roberson
on 5 Apr 2023
The example files both appear to use the same variable names. Are the variable names in the files all the same or do they differ? Because if they are all the same, there are other ways to approach this.
Accepted Answer
Walter Roberson
on 5 Apr 2023
readmatrix() of the file. Use rmmissing() to remove the header lines (which would have mostly translatted into NaN)
More Answers (1)
Oguz Kaan Hancioglu
on 5 Apr 2023
In both figures, the sturcture of each csv files are the same. Frequency and magnitude are in the same row. I suggest to read all csv files into a table. After that, you can find the index of the where the "frequency" text in the table. You can do cell or string comparision to find the index.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!