Read data and transfer comma to dot with detectImportOptions

15 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 14 Apr. 2023
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 Kommentare
Mark Sc
Mark Sc am 14 Apr. 2023
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 am 14 Apr. 2023
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 14 Apr. 2023
Verschoben: Walter Roberson am 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.

Kategorien

Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by