I need to import a .txt file that with numbers and words. In particular I need the first two numbers next to every "POINT" and the first number after "PATH", how can I do this?
I tried to import the .txt file using "Import Data" but this operation is too slow.
Thank you.

3 件のコメント

Akira Agata
Akira Agata 2017 年 12 月 14 日
Could you upload an example of your text file?
KL
KL 2017 年 12 月 14 日
Attach a dummy data file using the paperclip icon. That makes helping you easier.
Stephen23
Stephen23 2017 年 12 月 14 日
@Dario Anghileri: we cannot load a screenshot, or import it in any useful way. Even if someone patiently copied all of that data then it is unlikely that they would get the correct whitespace characters.
If you want help then please upload a real data file by clicking the paperclip button.

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

 採用された回答

YT
YT 2017 年 12 月 14 日
編集済み: YT 2017 年 12 月 14 日

1 投票

Here is my updated answer using delay.txt
clear all;
fid = fopen('delay.txt','r');
data = textscan(fid, '%s', 'Delimiter', '\n'); %read all data as string
data = data{1};
for i = 1:size(data,1)
nData{i,:} = strsplit(data{i},' '); %split data whitespace
if (mod(i,2)==1) %checks if uneven/POINT or even/PATH
nnData{i,1} = str2num(cell2mat(nData{i}(2))); %get POINT 1st number
nnData{i,2} = str2num(cell2mat(nData{i}(3))); %get POINT 2nd number
else
nnData{i,3} = str2num(cell2mat(nData{i}(2))); %get PATH 1st number
end
end
nnData = cell2mat(nnData); %make double matrix / removes empty rows
fclose(fid);

4 件のコメント

YT
YT 2017 年 12 月 14 日
編集済み: YT 2017 年 12 月 14 日
Use "fopen" with the correct path and filename:
You need to either have this:
fid = fopen('C:\user\project\data\delay.txt','r');
or something like this:
addpath('.\..\data');
fid =fopen('delay.txt','r');
Dario
Dario 2017 年 12 月 15 日
The solution you provide above works correctly with the first file.txt that I uploaded. (The first file "delay.txt" was a smaller file compared to the original file which is a 82 MB file.) The same solution doesn't work with the original file: the simulation runs for about 15 min. (I use a MacBook with i5, SSD and 4 GB RAM) and I think that in 15 min. I can import the file's data using 'Import Data'.
I tried to read the file by doing this:
fid = fopen('delay_ant1.txt');
C = textscan(fid, '%s%f%f%f');
fclose(fid);
In this quick way, I obtain 1x5 cell. In the 2nd and in the 3rd cell I have all the values I need. How can I improve this method or how I can extract the 3 arrays I need from theese cells?
YT
YT 2017 年 12 月 15 日
Okay so I tried it again, now without looping and with the code you provided in your comment
tic
fid = fopen('delay_ant.txt', 'r');
C = textscan(fid, '%s%f%f%f');
fclose(fid);
C2 = C{2}; C2(isnan(C2)) = []; %removing NaN
C2p2 = C2(2:2:end); %saving even rows
C2(2:2:end) = []; %remove even row
C3 = C{3}; C3(isnan(C3)) = []; %removing NaN
C3(2:2:end) = []; %remove even row
finalMatrix = [C2(:,1) C3 C2p2]; %[point1, point2, path1]
toc %clocked 0.22s
Dario
Dario 2017 年 12 月 15 日
Thank you so much!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Convert Image Type についてさらに検索

質問済み:

2017 年 12 月 14 日

コメント済み:

2017 年 12 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by