0

I have this data from a radiation diagram of an antenna in a txt file:

enter image description here

And the text continues. As you see, the integer and decimal part of numbers are separated by commas, instead of points. Moreover, I don't need the first row since they aren't values. I have tried using this code:

file = fread('file.txt')
data = fread(file)
fclose(file)

However, all the data was in a vector, so I tried to visualise a little part of the file, writing data = fread(file, [20,4]). Nevertheless, the data was wrong, here's what I obtained:

enter image description here

I also tried with fscanf function, but I didn't work either. I'd like to open the entire file (without knowing the number of elements previously).

I hope someone can help me. Thank you for your responses.

Josemi
  • 314
  • 1
  • 4
  • 20
  • AFAIK there is no way to set culture for reading file in Matlab. My advice would be to convert commas to dots by any mean (external editor, with matlab, etc) before importing. – Bentoy13 Feb 25 '19 at 15:51
  • @Bentoy13 no built-ins, true, but easy enough work arounds available, even in MATLAB, see the dupe target. No need to resort to using other programs in between (although obtaining your data with decimal dot would be preferable of course) – Adriaan Feb 25 '19 at 16:08

1 Answers1

3

You can specify the delimiter in many data reading functions so it's not a comma, I find readtable the simplest:

T = readtable( 'file.txt', 'Delimiter', 'tab' );

It looks like your data is tab separated, so use the 'tab' option. You could also use 'space' or any given character.

Comma-formatted decimals can then be converted

c = T.Properties.VariableNames;
for ii = 1:numel(c)
    t.(c{ii}) = cellfun( @(x)str2double(strrep(x,',','.')), t.(c{ii}) );
end
Wolfie
  • 27,562
  • 7
  • 28
  • 55