How to import one column from txt file

24 ビュー (過去 30 日間)
Dominika
Dominika 2014 年 3 月 7 日
コメント済み: Image Analyst 2014 年 3 月 8 日
Hi
I need to import one column from .txt file which contains only numbers like 1.54 1.28 ...
Which function does that?
Thanks

回答 (4 件)

Jos (10584)
Jos (10584) 2014 年 3 月 7 日
Take a look at at one of the import functions (textscan, load, etc.)
The easiest way is to import all the columns and then only keep the one you want to keep. For instance:
A = load ...
A = A(:,IndexOfColumnToKeep) ;

Image Analyst
Image Analyst 2014 年 3 月 7 日
You can use csvread if it's in csv format
M = csvread(filename,row,col)
Or use dlmread, importdata, textscan, fgetl, etc. depending on the format of the text file.
  1 件のコメント
Image Analyst
Image Analyst 2014 年 3 月 8 日
That file has such a big variety of formats that I'd probably just use allwords and fgetl(). Then build up a cell array of the words in a loop and then call xlswrite. In pseudocode
fid = fopen('S1P1.txt', 'r');
tline = fgetl(fid);
lineCounter = 1;
while ischar(tline)
disp(tline)
tline = fgetl(fid);
% Separate the line into separate words.
theWords = allwords(tline);
% Put the words into a cell array.
for k = 1 : length(theWords)
ca{lineCounter, k} = theWords{k};
end
end
xlswrite('text.xlsx', ca);
fclose(fid);
That's untested code just off the top of my head and it may need debugging (which I hope you know how to do).

サインインしてコメントする。


Dominika
Dominika 2014 年 3 月 8 日
Thanks for the answers.
I try to convert .txt to .xsls
file S1P1.txt looks like this:
"Norsonic RTA840"
"Ch1 LAST"
"Period=0"
"Frq[Hz]" "T20" "T30" "EDT" "S/N-MLS[dB]"
" 50" 1.54 1.45? 1.69 39.5
" 63" 1.28 1.46? 0.83 39.0
" 80" 1.35 1.43? 1.27 42.0
" 100" 1.28 1.35 1.20 45.8
" 125" 1.14 1.16 1.12 49.2
" 160" 0.92 0.92 1.10 45.4
" 200" 0.82 0.94 0.51 50.3
" 250" 0.33 0.44 0.47 50.2
" 315" 0.35 0.44 0.13 50.1
" 400" 0.29 0.36 0.20 47.4
" 500" 0.21 0.27 0.17 50.9
" 630" 0.16 0.21 0.14 55.8
" 800" 0.11 0.15 0.16 53.3
" 1k" 0.14 0.15 0.09 57.5
" 1.25k" 0.09 0.11 0.12 54.8
" 1.6k" 0.09 0.10 0.09 61.3
" 2k" 0.06 0.08 0.08 62.5
" 2.5k" 0.06 0.08 0.07 65.9
" 3.15k" 0.05 0.06 0.06 63.9
" 4k" 0.03 0.03 0.04 59.3
" 5k" 0.04 0.03 0.04 54.2
" L-netw" 0.11? 0.13? 0.10? 55.1
" A-netw" 0.09 0.11 0.10 60.3
"Setup information"
"Generated: Wed 19 Feb 2014 16:08:10"
"Measurement mode: Lvl-Flt-Mlt (Reverberation)"
"Master Instrument Mode: MLS"
"Register: LAST"
"Title:"
"No measurement title specified"
I tried with this
A = fileread('S1P1.txt') ;
data = textscan(A, '%s %f %f %f %f', 'delimiter', ' ') ;
data = [data{1}, num2cell([data{2:end}])] ;
xlswrite('S1P1.xlsx', data) ;
It creates file S1P1.xlsx but all is put into column A in excel. What I want is to have each column separately so that I can read then only column B for "T20". I guess the problem is 'delimiter'.
Thanks!
  1 件のコメント
per isakson
per isakson 2014 年 3 月 8 日
編集済み: per isakson 2014 年 3 月 8 日
  • Which lines are part of the file?
  • Why not using Excel?

サインインしてコメントする。


Dominika
Dominika 2014 年 3 月 8 日
"Norsonic RTA840"
"Ch1 LAST"
"Period=0"
"Frq[Hz]" "T20" "T30" "EDT" "S/N-MLS[dB]"
" 50" 1.54 1.45? 1.69 39.5
" 63" 1.28 1.46? 0.83 39.0
" 80" 1.35 1.43? 1.27 42.0
" 100" 1.28 1.35 1.20 45.8
" 125" 1.14 1.16 1.12 49.2
" 160" 0.92 0.92 1.10 45.4
" 200" 0.82 0.94 0.51 50.3
" 250" 0.33 0.44 0.47 50.2
" 315" 0.35 0.44 0.13 50.1
" 400" 0.29 0.36 0.20 47.4
" 500" 0.21 0.27 0.17 50.9
" 630" 0.16 0.21 0.14 55.8
" 800" 0.11 0.15 0.16 53.3
" 1k" 0.14 0.15 0.09 57.5
" 1.25k" 0.09 0.11 0.12 54.8
" 1.6k" 0.09 0.10 0.09 61.3
" 2k" 0.06 0.08 0.08 62.5
" 2.5k" 0.06 0.08 0.07 65.9
" 3.15k" 0.05 0.06 0.06 63.9
" 4k" 0.03 0.03 0.04 59.3
" 5k" 0.04 0.03 0.04 54.2
" L-netw" 0.11? 0.13? 0.10? 55.1
" A-netw" 0.09 0.11 0.10 60.3
"Setup information"
"Generated: Wed 19 Feb 2014 16:08:10"
"Measurement mode: Lvl-Flt-Mlt (Reverberation)"
"Master Instrument Mode: MLS"
"Register: LAST"
"Title:"
"No measurement title specified"
Everything above is exactly what .txt file contains. What do you mean by using Excel? This is what I'm trying to do, I try to convert it to excel file
Thanks :)

カテゴリ

Help Center および File ExchangeStandard File Formats についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by