How to read data from a .txt file matlab [duplicate]

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.

2

1 Answer

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

T = readtable( 'file.txt', 'Delimeter', '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
0

You Might Also Like