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

 채택된 답변

Stephen23
Stephen23 2019년 1월 24일
편집: Stephen23 2019년 1월 24일

0 개 추천

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

Bass Tea
Bass Tea 2019년 1월 24일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2019년 1월 24일

0 개 추천

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

댓글 수: 2

Bass Tea
Bass Tea 2019년 1월 24일
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 2019년 1월 24일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Text Data Preparation에 대해 자세히 알아보기

제품

릴리스

R2015a

질문:

2019년 1월 24일

댓글:

2019년 1월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by