How to assign NaN to an empty cells in array for data arrangement?

조회 수: 1 (최근 30일)
arifahazwani AY
arifahazwani AY 2021년 3월 4일
댓글: KALYAN ACHARJYA 2021년 3월 5일
Hi,
Currently, I'm trying to arrange my data in .txt. file of certain column in an .xls file.
But I got this error, as the dimension of each selected column of the .txt file are not similar.
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
I plan to assign NaN to the empty cell to obtain similar dimension of each data.
What are the best solution for this problem?
Attached herewith the .txt. file and command I had tried.
S1 = importdata ('s1.txt');
%Read the data
GRFX1 = S1.data(:,2);
GRFY1 = S1.data(:,3);
GRFZ1 = S1.data(:,4);
S2 = importdata ('S2 0.txt');
%Read the data
GRFX2 = S2.data(:,2);
GRFY2 = S2.data(:,3);
GRFZ2 = S2.data(:,4);
S_1 = [GRFX1,GRFY1,GRFZ1];
S_2 = [GRFX2,GRFY2,GRFZ2];
GRFX=[GRFX1, GRFX2];
GRFY=[GRFY1, GRFY2];
GRFZ=[GRFZ1, GRFZ2];
%Arrange the data in one sheet before convert to .xls file
N = [S_1, S_2];
GRF = [GRFX, GRFY, GRFZ];
Thank you in advance!

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 3월 4일
편집: KALYAN ACHARJYA 2021년 3월 4일
Please try with readtable, it replaces all missing data with NaN.
Note: Text files have unwanted header descriptions, you can delete them so that there is no problem when analyzing both files. However, if both files have different length data (originally), then you have the following options
  • Add NaN during the beginning or end of the data (like pad array concept)
  • You can then interpolate data in between to make the same length.
  • Delete (Peridic wise) data from a larger one, so that both files fit equal.
  댓글 수: 2
arifahazwani AY
arifahazwani AY 2021년 3월 5일
Thank you for your response.
Perhaps, could you please share the coding script as, I'm not sure how to use readtable.
Thank you!
KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 3월 5일
S1=readtable('s1.txt');
See the table to access any rows or columns.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by