Merge numeric values of 2 columns into 1

3 vues (au cours des 30 derniers jours)
Andrea Boa
Andrea Boa le 16 Juin 2020
Commenté : Robert U le 16 Juin 2020
Hello,
I am importing data from COMSOL Multiphysics to Matlab, and the data is imported wrong. Some values are divided into different cells when they should not, and the second cell contains the information for the next value. For example: the columns [-4.83853986927724E-7, -5.796128574671958E-7] show in 2 different columns as [-4.838539869 , 27724E-7 -5.796128574671958E-7]
I would like to divide the second column (withouth changing the value 27724E-7 to 2.7724E-3 - so maybe using strings?) and join such value to the previous column. Does anybody know how?
Thanks!
  2 commentaires
madhan ravi
madhan ravi le 16 Juin 2020
Could you attach the data?
Andrea Boa
Andrea Boa le 16 Juin 2020
The data is quite large. There are several columns showing this error, although the ones I used as example are in columns KX and KY.

Connectez-vous pour commenter.

Réponses (1)

Robert U
Robert U le 16 Juin 2020
Hi Andrea Boa,
probably your import function is not correct regarding either format or delimiter, or both. The following function converts the given data into a double-matrix. I hope it serves your needs.
function [dataOut] = importComsolTable(strFileName)
fID = fopen(strFileName);
dataIn{1,1} = fgetl(fID);
while ischar(dataIn{end})
dataIn{end+1,1} = fgetl(fID);
end
dataIn = dataIn(1:end-1,1);
fclose(fID);
Data = dataIn(cellfun(@isempty,regexp(dataIn,'%%*')));
Data = cellfun(@(strIn)strsplit(strIn,char(9))',Data,'UniformOutput',false);
dataOut = str2double([Data{:}]');
end
Function test call:
Test = importComsolTable('Emy.txt');
Kind regards,
Robert
  3 commentaires
Robert U
Robert U le 16 Juin 2020
Modifié(e) : Robert U le 16 Juin 2020
Now, I see. Your txt file is corrupted.
Robert U
Robert U le 16 Juin 2020
function [dataOut] = importComsolTable(strFileName)
fID = fopen(strFileName);
dataIn{1,1} = fgetl(fID);
while ischar(dataIn{end})
dataIn{end+1,1} = fgetl(fID);
end
dataIn = dataIn(1:end-1,1);
fclose(fID);
Data = dataIn(cellfun(@isempty,regexp(dataIn,'%%*')));
Data = cellfun(@(strIn) regexprep(strIn,'\t(?=\d*[E]([+]|[-])\d+)',''),Data,'UniformOutput',false);
Data = cellfun(@(strIn) regexprep(strIn,sprintf('%s{3,4}',char(32)),'\t'),Data,'UniformOutput',false);
Data = cellfun(@(strIn)strsplit(strIn,char(9))',Data,'UniformOutput',false);
dataOut = str2double([Data{:}]');
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by