MATLAB Answers

Get horizontal (row) data from a txt file (table) with a reguler-pattern rows (series)

9 views (last 30 days)
Tyann Hardyn
Tyann Hardyn on 17 Jul 2021
Commented: Tyann Hardyn on 4 Sep 2021 at 13:03
Hi everyone, i wanna ask about how to get a horizontal data from a txt file with a reguler series. So i have this kind of data (with a new header for every 30 row) :
I want to get the data in green rectangular (row 2, column 1) as 1st data, data in orange rectangular (row 2, column 5) as 2nd data, data in black rectangular (row 3, column 1) as 3rd, data in purple rectangular (row 3, column 5) as 4th data, and continuously loop with this pattern until it stopped at (row 30, column 5) as final data so that make total 60 data (1st data, 2nd data.... 60th data) before it get reach a new header again. I want make the withdrawed data to become just 1 column like this :
Horizontal Component
1st data
2nd data
3rd data
4th data
.
.
.
60th data
So then i can plot it as a time dependent curve. But firstly, i need to get a script how to get or convert that kind of data to become a column data like that.... I want to get the data from uigetfile function :
[filename,direct]=uigetfile({'*.txt', 'Text-files (*.txt)'},'Load IMF Magnet Data');
full = fullfile(direct, filename);
The data i gave before was a .imf formatted, but it doesnt matter as it can be converted from .imf to .txt before, so i can get the .txt file from uigetfile until it can be writted again in a new file with :
[savefile, direct, default] = uiputfile('*.txt', 'Save As', 'New Data');
eval(['cd ''' direct ''';']);
fout=fopen(savefile,'w');
. But i didnt know how to handle the data after uigetfile function was started to get the data i need (its kinda too difficult to me)....
Would you mind to help my problem here. Thank you very much everyone....

Accepted Answer

Simon Chan
Simon Chan on 17 Jul 2021
Use readmatrix to retrieve the data in the file as follows:
B = readmatrix(full);
Data1 = B(1:2:30,1);
Data2 = B(1:2:30,5);
Data3 = B(2:2:30,1);
Data4 = B(2:2:30,5);
Data=[Data1, Data2, Data3, Data4]';
Data = Data(:);
plot(Data)
  12 Comments
Tyann Hardyn
Tyann Hardyn on 4 Sep 2021 at 13:03
@Simon Chan I want to ask, Sir. Do you have any idea to create that logic of readmatrix into a readtable instead, Sir? Because i want to read a multifile data and it will not work if using readmatrix....

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by