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

采纳的回答

Cris LaPierre
Cris LaPierre 2023-4-14
I would use readmatrix and specify the DecimalSeparator.
d_lops = readmatrix('trial.txt','DecimalSeparator',',')
d_lops = 2×3
1.1000 3.0000 4.0000 3.1200 5.4500 2.0000
  2 个评论
Mark Sc
Mark Sc 2023-4-14
Thank you so much for your answer, but would it be possible to use opts.SelectedVariableNames
I also wanna learn how to use this function with import data ?
Cris LaPierre
Cris LaPierre 2023-4-14
It would be. You need to create an import options object first. Maybe like this?
opts = detectImportOptions('trial.txt','DecimalSeparator',',');
opts.SelectedVariableNames = ["Var1","Var2"];
d_lops = readmatrix('trial.txt',opts)
d_lops = 2×2
1.1000 3.0000 3.1200 5.4500

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
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.

类别

Help CenterFile 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!

Translated by