Alias blanks in the cell array as a string.

7 ビュー (過去 30 日間)
Viraj Sabane
Viraj Sabane 2025 年 1 月 31 日
回答済み: Viraj Sabane 2025 年 2 月 16 日
I am using the ‘importdata’ function to read a text file. It reads the numeric data as a matrix and text data as a cell array; now the first element of cell array is all the headers, I use a ‘strsplit’ function to separate each individual variable name in header. The issue is that there are some blank columns in the text file, so the numeric matrix reads it as a ‘NaN’ but the resulting variables (which I used ‘strsplit’ for) eliminates blanks. Now the result is that size(textdata,2) +size(data,2) is not equal to size(variable,2). I want to read blanks in the header as a variable, alias it as ‘empty’
  5 件のコメント
Stephen23
Stephen23 2025 年 1 月 31 日
I second dpb's comment: avoid IMPORTDATA, use READTABLE or TEXTSCAN or the like.
Viraj Sabane
Viraj Sabane 2025 年 2 月 3 日
Thank you all. I will experiment with 'readtable' and 'textscan' some more. Really appreciate this community for encourging the correct way of programming.

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2025 年 1 月 31 日
As the OP found, the solution is to include multiple delimiters
strsplit(t,'\t', 'CollapseDelimiters',false)
And as others have recommended, using readtable offers better control of how data are read than importdata.
  1 件のコメント
Viraj Sabane
Viraj Sabane 2025 年 2 月 3 日
Thank you, Adam.

サインインしてコメントする。

その他の回答 (1 件)

Viraj Sabane
Viraj Sabane 2025 年 2 月 16 日
Hello All, taking advice of everyone who commented; I was able to robustly solve the issue of dowloading data from a text file and also retain the variable name. This particular text file had variable names in line 1 and line line 1 also had blanks. Following code has worked reliably for me.
And if your text file happen to have many lines before the line with headers, following has worked well for me.

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by