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

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....

 Respuesta aceptada

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 comentarios

Tyann Hardyn
Tyann Hardyn el 17 de Jul. de 2021
Editada: Tyann Hardyn el 17 de Jul. de 2021
Ohhh, thats so great solution, but did you write :
Data1 = B(1:2:30,1);
Data2 = B(1:2:30,5);
Data3 = B(2:2:30,1);
Data4 = B(2:2:30,5);.... Data60 = B(30:2:30,5);
Data=[Data1, Data2, Data3, Data4.... Data60]';
Until it get reach 60 datas like that to create the curve sir? or could it be more simplified again?
The above code is able to generate the attached figure, there is no need to assign Data1 to Data60.
Data1 and Data2 stores all data in alternate row from 1 to 29 (row 1,3,5,...,29, but we can use 30 in the code) with column 1 and column 5 respectively. Similarly, Data3 and Data4 stores all data in alternate row from 2 to 30 (row 2,4,6,...,30). Hence each of them contains 15 data.
These 4 column matrices are then concantenate horizontally and becomes a 15x4 matrix (so already 60 data).
Since you would like to get a column matrix and in the order from data1 to data60, so I need to transpose the matrix and use Data(:) to make it to a column matrix.
Thats awesome, im very grateful to your answer its help me a lot, Sir. Thank you very much.
And one more Sir, ummm.... About the data series in a new header below 30th row and more, how could i merge them all with the first script you gave me? Let me try... could it be like this ??
Data1 = B(32:2:30,1);
Data2 = B(32:2:30,5);
Data3 = B(33:2:30,1);
Data4 = B(33:2:30,5);
Data=[Data1, Data2, Data3, Data4]';
Data = Data(:);
plot(Data)
Since a new header would be created for every 30 row, to be exact in this case the header would be created in 31th row, so the data will be started again in 32th row, right?
Yes, you need to start from row 32, but this is not a good method to extract the rest of the data.
So review the matrix B and you can find NaN occurs on the header row, such as row 31, so B(31,:) gives
NaN NaN 62 1 NaN NaN NaN 8881249 485 NaN
Knowing that column 1, 2, 5, 6 and 7 on the header rows are NaN, I can find the row index for the header rows. I choose column 1 just an example because this is enough for me to find all header rows.
After that I remove all the header rows from the original matrix
idx=isnan(B(:,1)); % Find the index for header rows
B(idx,:)=[]; % Delete the header rows
After this operation, there is no more header rows in the matrix, and you are able to retrieve data start from row number 1, 31, 61,...,691. (Matrix B without header rows has 720 rows)
Then retrieve the next data like this (provided all header rows are removed):
Data1 = B(31:2:60,1);
Data2 = B(31:2:60,5);
Data3 = B(32:2:60,1); % Should be 32 instead of 31
Data4 = B(32:2:60,5); % Should be 32 instead of 31
Alright, Sir. So if i want to retrieve the whole data (720 rows) in that .imf file i can just use this code ?
B = readmatrix(full);
Data1 = B(31:2:60,1);
Data2 = B(31:2:60,5);
Data3 = B(31:2:60,1);
Data4 = B(31:2:60,5);
Data=[Data1, Data2, Data3, Data4]';
idx=isnan(B(:,1)); % Find the index for header rows
B(idx,:)=[]; % Delete the header rows
Data = Data(:);
plot(Data)
And yes Sir, the total row must be 720 because the total data should be represent the amount of minutes in 1 day, that is 1440 minutes, and each row represent 2 minutes data...
Or maybe it become like this, Sir?
B = readmatrix(full);
Data1 = B(1:2:743,1);
Data2 = B(1:2:743,5);
Data3 = B(2:2:743,1);
Data4 = B(2:2:743,5);
Data=[Data1, Data2, Data3, Data4]';
idx=isnan(B(:,1)); % Find the index for header rows
B(idx,:)=[];
Data = Data(:);
time = 1:1:1440;
plot(time, Data);
There was a typo error on my last comment, where row number for Data3 and Data4 should be 32 instead of 31.
ANyway, if you wwould like to handle the entire data at one time, it is better to use for loop as follows:
B = readmatrix(full);
idx=isnan(B(:,1)); % Find the index for header rows
B(idx,:)=[]; % Delete the header rows
Nrow = size(B,1); % Total number of rows in the matrix
Nanalysis = Nrow/30; % Total number of analysis for the entire file
for k = 1:1:Nanalysis
Data1 = B((k-1)*30+1:2:k*30,1); % Extract odd number of rows at column 1
Data2 = B((k-1)*30+1:2:k*30,5); % Extract odd number of rows at column 5
Data3 = B((k-1)*30+2:2:k*30,1); % Extract even number of rows at column 1
Data4 = B((k-1)*30+2:2:k*30,5); % Extract even number of rows at column 5
Data=[Data1, Data2, Data3, Data4]';
Data = Data(:);
subplot(4,6,k)
plot(Data)
grid on
title(sprintf('Aanlysis #%d',k))
end
Thats awesome, Sir.... But, could it be possible to create just one plot to represent the whole data in the text file? I mean, not shown as separeted subplot like that... Because i want to manage the data to be written again with this new format :
TIME MAGNET COMPONENT
1 Data1
2 Data2
3 Data3
4 Data4......... Data1440
........ 1440
by using uiputfile function like this below, Sir :
time = 1:1:1440;
[savefile, direct, default] = uiputfile('*.txt', 'Save As');
eval(['cd ''' direct ''';']);
fout=fopen(savefile,'w');
for i=1:length(time)
fprintf(fout,'%f %.2f\n', time, Data);
end
fclose(fout);
Please, could it be possible, Sir? /.\
This is even simpler...
B = readmatrix(full);
idx=isnan(B(:,1)); % Find the index for header rows
B(idx,:)=[]; % Delete the header rows
Data1 = B(1:2:end,1); % Extract odd number of rows at column 1
Data2 = B(1:2:end,5); % Extract odd number of rows at column 5
Data3 = B(2:2:end,1); % Extract even number of rows at column 1
Data4 = B(2:2:end,5); % Extract even number of rows at column 5
Data=[Data1, Data2, Data3, Data4]';
Data = Data(:);
plot(Data)
grid on
%
time = 1:1:1440;
[savefile, direct, default] = uiputfile('*.txt', 'Save As');
eval(['cd ''' direct ''';']);
fout=fopen(savefile,'w');
for i=1:length(time)
fprintf(fout,'%f %.2f\n', time(i), Data(i));
end
fclose(fout);
Tyann Hardyn
Tyann Hardyn el 18 de Jul. de 2021
Editada: Tyann Hardyn el 18 de Jul. de 2021
OMG, Sir... Im so happy... Im very satisfied.... Incredible, Awesome.... I have to learn more about a lot of matlab function and then i can be like you, helping anyone else in the forum^^. Im sure you re familiar with matlab code or function in a long time. Im glad to learn with you. So, are you a member of matlab Developer Team? hehehehehe. Thanks a lot Sir, it solved my question, finally !!
No, I am also learning.
@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....

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 17 de Jul. de 2021

Comentada:

el 4 de Sept. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by