How can I remove quotes from a table?

10 views (last 30 days)
Mohammad Haque
Mohammad Haque on 12 Jul 2018
Commented: dpb on 22 Jul 2018
Hi,
I have a large csv file which I import to MATLAB by an user-prompt and store the information in a table. After storing the information in the table, I see that each value in the table gets appended inside a single quotation mark. How to remove this quotation mark from each element or more specifically, how can I avoid storing the information from the csv file without appending a quotation mark with every element? Screenshot & code attached.
BaseDir = 'C:\Users\mhaque7\Documents\MATLAB'; % Set as you need
[FileName, FilePath] = uigetfile('*.csv', ...
'Please choose an Excel file', BaseDir);
if isequal(FileName, 0)
disp('User aborted file choosing.');
return; % Assuming this is a function
end
File = fullfile(FilePath, FileName);
data_src=readtable(File) %importing data from the csv file into a table

Accepted Answer

dpb
dpb on 12 Jul 2018
Because you used default conditions to readtable and didn't give it enough help to parse the data correctly; hence it read it all as cellstr--what the quotes are showing. They're not part of the data; they're just a visual cue ML uses to inform of what the data types are.
I built a sample of your file and read it as follows:
>> opt=detectImportOptions('haque.csv')
opt =
DelimitedTextImportOptions with properties:
Format Properties:
Delimiter: {','}
...
Variable Import Properties: Set types by name using setvartype
VariableNames: {'Time', 'x_DB_SEC', 'x_DB_USEC' ... and 1 more}
VariableTypes: {'double', 'double', 'double' ... and 1 more}
SelectedVariableNames: {'Time', 'x_DB_SEC', 'x_DB_USEC' ... and 1 more}
VariableOptions: Show all 4 VariableOptions
Access VariableOptions sub-properties using setvaropts/getvaropts
Location Properties:
DataLine: 3
VariableNamesLine: 1
RowNamesColumn: 0
VariableUnitsLine: 0
VariableDescriptionsLine: 0
opt.VariableOptions =
1x4 VariableImportOptions array with properties:
Variable Options:
(1) | (2) | (3) | (4)
Name: 'Time' | 'x_DB_SEC' | 'x_DB_USEC' | 'HRSTEST'
Type: 'double' | 'double' | 'double' | 'double'
FillValue: NaN | NaN | NaN | NaN
TreatAsMissing: {} | {} | {} | {}
QuoteRule: 'remove' | 'remove' | 'remove' | 'remove'
NB: the scan routine did figure out the variable names are in record 1 and the data starts at 3 and so recognized it as numeric. That's a big start. It did not, however, assign the second row to be the variable units. We can fix that pretty easily, however--
>> opt.VariableUnitsLine=2; % use 2nd record for units
>> t=readtable('haque.csv',opt) % _now_ we can read with this helper object
t =
3×4 table
Time x_DB_SEC x_DB_USEC HRSTEST
_____ ________ _________ _______
0 0 0 0
0.001 0 1000 0
0.002 0 2000 0
>> t.Properties.VariableUnits
ans =
1×4 cell array
{'sec'} {'sec'} {'usec'} {'hour'}
>>
and Voila! with the extra help you have both the units available and the numeric values as native doubles.
  12 Comments
Mohammad Haque
Mohammad Haque on 22 Jul 2018
Thanks for your feedback. I guess I have to use 'subplot' function then instead of crowding the plot with multiple y-axes. In fact it would be more understandable to plot individual variables on a separate plot and with their respective values on a single y-axis. For comparison, I could probably select two variables on the same x-axis and two separate y-axes. For the x-axis values however, my concern is to see the values in the non-scientific form. Right now, the x-axis scale appears to be in exponent form of 10 such as 2X10^4. Which is why I specifically set the x-axis limit as the whole number since I have looked at the raw file. I just wanted to avoid it without opening the raw file & let MATLAB plot show me in a whole number form if that makes any sense. By the way, somewhere on the forum, I saw someone mention a toolbox called 'plt' in MATLAB which enables you to select multiple channels on the plot but I couldn't find any. Is there such a plotting toolbox in MATLAB which allows you to view multiple channels on the same x axis?
dpb
dpb on 22 Jul 2018
hAx=gca;
hAx.XAxis.Exponent=0;
I don't know what you envision by " view multiple channels on the same x axis". You can plot as many lines on a given axis as you wish.
There are many plotting submissions on FEX for various things; perhaps something there may have been what was being referred to; I've no klew...

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!