another import from textfile question

dear ladies and gentlemen
of course this frequently occuring questions about data insertion are very annoying to you, i can understand that, but have to put my issue in here... =(
i have a textfile with maaaaaaaaany columns (2 rows and 13000 columns). its generated from a labview program. unfortunately i am not able to code a formatspec for that amount of columns. so cannot manage to make the data import working.
the data in my text.file look like this. actually they are seperated by \t ...
1.JPG
but e.q. a .....
filename = 'S:\...\M0905_PK6.txt';
delimiterIn = '\t';
M = importdata(filename, delimiterIn);
...only creates a 2x1 cell where all values are consequentivly written in one cell per row ..
2.JPG
if i'm trying things like that....
...
[A,count] = fscanf(fileID, '%f %f');
...
..how can i handle the formatspec of so many columns.
of course i could transform (transpose, what is my original intention) those files manually with notepad++ e.q. per search and replace but i wanted to script it, maybe additional with a file location prompt later
i'm sending the original file and txtfile enclosed
can someone give me a hint =) ?
kindly regards
Basti

 Respuesta aceptada

Stephen23
Stephen23 el 24 de En. de 2019
Editada: Stephen23 el 24 de En. de 2019

0 votos

The main difficulty with your file is that it uses a decimal comma. MATLAB supports only a decimal point. You can search this forum for "decimal comma" to find various solutions, but essentially it boils down to either:
  1. altering the file by swapping comma for point before parsing with MATLAB, or
  2. importing as character, swap comma for point, convert to numeric.
The first solution you can figure out yourself.
The second solution can be achieved in many ways, here is one which uses efficient sscanf:
str = fileread('M0905_PK6.txt');
str = strrep(str,',','.');
mat = sscanf(str,'%f');
mat = reshape(mat,[],2).';
Giving:
>> mat(:,1:8) % first eight columns
ans =
0.23343 0.23347 0.23347 0.23353 0.23343 0.23350 0.23340 0.23350
-0.19354 -0.19354 -0.19350 -0.19360 -0.19350 -0.19354 -0.19354 -0.19363
>> mat(:,end-7:end) % last eight columns
ans =
0.23038 0.23038 0.23038 0.23045 0.23051 0.23061 0.23058 0.23067
-0.19324 -0.19334 -0.19324 -0.19328 -0.19321 -0.19318 -0.19315 -0.19318

1 comentario

Bass Tea
Bass Tea el 24 de En. de 2019
hey stephen,
you are brilliant. this is all what i need. i have read yesterday such things like "first import as a string, then repair, then transform" but wasn't able to make it working, as well.
i really really thank you very much. the first method u mentioned, clearly make sense, but i wanted to pass that =) so method 2 is great.
2.jpg
appreciate it. have a good one

Iniciar sesión para comentar.

Más respuestas (1)

Guillaume
Guillaume el 24 de En. de 2019
Your file is a simple tab delimited file that is trivially read by csvread or dlmread or readtable:
M = csvread('M0905_PK6.txt')

2 comentarios

Bass Tea
Bass Tea el 24 de En. de 2019
hi
i really thank you very much for your hint. while i'm doing so, i got such output:
1.JPG
so it's almost done, besides that there is still the problem with my comma decimal seperation sign. how can i tell matlab to only focuss on the \t with a csvread command. matlab must not use comma as a seperater, only \t
regards
Guillaume
Guillaume el 24 de En. de 2019
Oh! I completely missed that it had commas instead of dots for the decimal separators. It's unfortunate that it's still something that matlab doesn't handle properly.

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2015a

Preguntada:

el 24 de En. de 2019

Comentada:

el 24 de En. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by