Filter löschen
Filter löschen

Import text file with different column lengths

20 Ansichten (letzte 30 Tage)
Cory Powell
Cory Powell am 12 Mai 2017
Kommentiert: Emu am 15 Nov. 2023
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

Antworten (3)

Walter Roberson
Walter Roberson am 12 Mai 2017
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 Kommentare
Walter Roberson
Walter Roberson am 19 Mai 2017
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 am 15 Nov. 2023
This worked for me !

Melden Sie sich an, um zu kommentieren.


Vincent Scalfani
Vincent Scalfani am 14 Jul. 2018
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 am 10 Jan. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by