Read data and transfer comma to dot with detectImportOptions
16 views (last 30 days)
Show older comments
Hi All,
I would like to read a data file, I would like to transfer data with comma to dot. I have read on the internet a lot of answers..however, I should use importdata... so I know how to read them, but is there is a way to transfer the comma to dot without create a new file as suggested by previous answer of Jan...
I attached a file with very small data, and proposed code..
Hope anyone code help,.
clearvars;
clc;
filename = 'trial.txt';
delimiterIn = '\t';
headerlinesIn = 3;
d_lops = importdata(filename,delimiterIn,headerlinesIn);
opts.SelectedVariableNames = {',','.'};
%% I even tried with the following
numeric_data=str2num(char(strrep(d_lops.data(1,1),',','.')))
%numericData = [];
% for k=1:size(d_lops ,1)
% numericData(k,:) = str2num(char(strrep(data(k,:),',','.')));
% end
0 Comments
Accepted Answer
Cris LaPierre
on 14 Apr 2023
d_lops = readmatrix('trial.txt','DecimalSeparator',',')
2 Comments
Cris LaPierre
on 14 Apr 2023
opts = detectImportOptions('trial.txt','DecimalSeparator',',');
opts.SelectedVariableNames = ["Var1","Var2"];
d_lops = readmatrix('trial.txt',opts)
More Answers (1)
Walter Roberson
on 14 Apr 2023
Moved: Walter Roberson
on 14 Apr 2023
No. importdata() does not support any options other than delimiter and number of header lines.
You do not have an existing variable named opts so the assignment
opts.SelectedVariableNames = {',','.'};
is just creating a struct named opts with a field named SelectedVariableNames and assigning the cell array of character vectors to it. You then do not use opts anywhere.
SelectedVariableNames suggests strongly that you are looking at code or documentation involving using detectImportOptions (or their more specific variants for different file types.) Those objects are used with readtable() or readcell() or readmatrix() or readtimetable() . The SelectedVariableNames option for those has to do with which columns are to be imported, and has nothing to do with which character to use as decimal. It is the DecimalSeparator property of those objects that controls which decimal character to use.
If you want to avoid reading a file first, changing the separator using text manipulation, writing to a temporary file, and then importing the temporary file, then your options are:
- use readtable() or readcell() or readmatrix() or readtimetable() with the DecimalSeparator option; Or
- read the text file using fileread(), use text manipulation to change the separator, and then call textscan on the resulting character vector.
0 Comments
See Also
Categories
Find more on Large Files and Big Data 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!