How to remove apostrophes from empty cells when using readtable

17 views (last 30 days)
I used readtable to import a csv file and any blank cells, including non-blank cells following, are automatically filled with apostrophes or have apostrophes surrounding a value. With the apostrophes surrounding a value I am not able to plot them. I was wondering what the best way is to either completely get rid of the apostrophes (converting to NaN) or how to make it so they only affect blank cells, and not the cells following.

Accepted Answer

Walter Roberson
Walter Roberson on 22 May 2023
NewTable = convertvars(convertvars(YourTable, @iscellstr, 'string'), @isstring, 'double');
This would convert the '' into NaN -- and would also convert the 'S03:RLEK' and the 'Z' and 'mm' to NaN.
Afterwards all columns that were cell array of character vectors or were string arrays, will be converted to numeric.
At that point you would probably want to consider doing filtering, such as looking for rows of all NaN as marking the boundaries between sections in the table.
  2 Comments
Walter Roberson
Walter Roberson on 22 May 2023
Also since you are using readtable():
You can start by using detectImportOptions . Then use setvartype to set those variables to 'double'. Now when you readtable you will get numeric columns there, with NaN for the empty locations and the non-numeric text.
The disadvantage compared to what you are doing now is that with what you are doing now you can search the text looking for things like the 'S03:RLEK' -- though if that is useful or important for your purposes, I would recommend setvartype to 'char' for those columns, as at present we can suspect that (for example) column 42 had text in it, probably an S03 and an 'X', that was set to NaN because there were enough numeric entries in the column that readtable() guessed the entire column should be numeric and the 'X' was noise / error.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 22 May 2023
Why are you trying to plot them? Shouldn't you just use table2array to extract the numbers and plot only the numbers (shown in the lower part of your table)? Also, you forgot to attach your table. In a table, all items in a table column must be of the same type. Since your columns mix numbers and strings, it made everything a string since that is the only type that would work for every item in the column. Maybe you should tell readtable to skip some lines (via the NumHeaderLInes option) in your file to avoid that problem. Then everything would be numbers. Of course we can't try anything because you forgot to attach your data file.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!