简体   繁体   中英

Reading text files, error using load MATLAB

I have a text file containing my data looking like this:

3,848E-05
3,848E-05
3,848E-05
3,848E-05
3,848E-05
3,848E-05
3,848E-05
3,848E-05
3,848E-05 
2,3088E-05
-0,00013468

I have tried using load and get an error telling me that the ASCII file must contain the same number of columns per lines.

fname1 = fullfile(path,'test1.txt');
emg1 = load(fname1);


Error using load
Number of columns on line 233 of ASCII file must
be the same as previous lines.

I've also tried using importdata and that couldn't deal with this kind of number format. Any ideas? I'm using MATLAB 2014a.

fname1 = fullfile(path,'test1.txt');
emg1 = importdata(fname1);

This is how the imported data looked like using importdata.

3   0.00848000000000000
3   0.00848000000000000
3   0.00848000000000000
3   0.00848000000000000
3   0.00848000000000000
3   0.00848000000000000
3   0.00848000000000000
3   0.00848000000000000
3   0.00848000000000000
2   0.0308800000000000
-1  0.00924000000000000

The problem seems due to the fact that the values in the file contains , (comma) instead of . (dot).

In this cse, you can

  • read the input file as a text file (containing string)ù
  • replace the , with .
  • convert the string to number (double)

A possible solution could be:

% Open the input file
fp=fopen('d.txt','rt')
% Read the file as a text file
C=textscan(fp,'%s')
% Close the text file
fclose(fp)
% Convert the string to numbers
x=str2num(char(strrep(C{1}(:),',','.')))

Hope this helps.

Qapla'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM