Filter löschen
Filter löschen

How to read and take a part of data of a text file in MATLAB

1 Ansicht (letzte 30 Tage)
Phan
Phan am 20 Aug. 2014
Kommentiert: Phan am 21 Aug. 2014
Dear everyone,
Please help me to solve this problem. I need to make a code that can read data from a text file. The format of the file is as follows:
0.1 ABC63-820
0.5 ABC63-818
1.0 ABC63-813
I want to take out two variables like this:
X=[0.1 0.5 1.0];
Y=[20 18 13]
Can anyone help me to write a code to take out these data?
Thank you so much!

Akzeptierte Antwort

Michael Haderlein
Michael Haderlein am 20 Aug. 2014
Dear Phan,
textscan can do the job:
>> fid=fopen(filename);
>> data=textscan(fid,'%f ABC63-8%d');
>> fclose(fid);
>> x=data{1};y=data{2};
Best regards,
Michael
  3 Kommentare
Michael Haderlein
Michael Haderlein am 20 Aug. 2014
It would have been easier if you'd said that right from the beginning.
So now, we first read the second part as string and then split it. There might be different ways to split, but this one will work:
fid=fopen('test1.txt');
data=textscan(fid,'%f %s');
fclose(fid);
x=data{1};
[Z,y]=cellfun(@(x) deal(x(1:end-2),str2num(x(end-1:end))),data{2},'uniform',false);
Y=cell2mat(y);
Phan
Phan am 21 Aug. 2014
Thank you so much! I did it!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Import and Export finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by