Read data and transfer comma to dot with detectImportOptions
15 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Cris LaPierre
2023-4-14
d_lops = readmatrix('trial.txt','DecimalSeparator',',')
2 个评论
Cris LaPierre
2023-4-14
opts = detectImportOptions('trial.txt','DecimalSeparator',',');
opts.SelectedVariableNames = ["Var1","Var2"];
d_lops = readmatrix('trial.txt',opts)
更多回答(1 个)
Walter Roberson
2023-4-14
移动:Walter Roberson
2023-4-14
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Large Files and Big Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!