Import text file with different column lengths

조회 수: 28 (최근 30일)
Cory Powell
Cory Powell 2017년 5월 12일
댓글: Emu 2023년 11월 15일
Hello,
This is a two part question:
1. Is it possible to import data from a .txt file with varying column lengths?
2. With that, is it possible to create a plot to which the lengths of data entries are not the same?
Scenario: I have three different load vs deflection tests but I want them all on the same plot. Each specimen failed at different times creating the various column dimensions between them all. Each data set has been imported to a separate MatLab code, adjusted, then exported to an .xlsx file. I have then put them all into one .xlsx file and created a .txt file from that and am now attempting to import them all and plot them on one plot.
If more clarity is needed please let me know.
Thank you ahead of time, Cory

답변 (3개)

Walter Roberson
Walter Roberson 2017년 5월 12일
It would be easier to process the .xlsx files, even if there were columns of different lengths in them.
Failing that, if there is a consistent delimiter between columns, then you can use textscan, perhaps together with TreatAsEmpty
If you have spaces between columns instead of using delimiters between columns, and the missing entries are implied by the vertical alignment, then it becomes more difficult to import. In such a case, sometimes the easiest approach is to read the file as a single string, split into rows as a cellstr, then char() the cellstr so you get a char array, and then extract fixed column widths from the char array, in order to be able to deduce that the blanks mean "no entry"
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 5월 19일
With R2013b or later, you can read the simple sample irregular xlsx I give here with
data = readtable('irregular_columns.xlsx','ReadVariableNames',false)
the result is a table() object that has nan for the missing entries.
plot(data{:,:})
Emu
Emu 2023년 11월 15일
This worked for me !

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


Vincent Scalfani
Vincent Scalfani 2018년 7월 14일
Hi, this worked for me. Data file is read as a string, then each line is split into cell rows.
% Import data
data = fileread('data.txt');
data = cellstr(data);
% split at newline character
data = cellfun(@(newline) strsplit(newline, '\n'), data, 'UniformOutput', false);
% reposition split values into individual cells
% horizontal concatenation
data = [data{:}];
% transpose
data = data';

Hossam Etman
Hossam Etman 2020년 1월 10일

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by