Info

此问题已关闭。 请重新打开它进行编辑或回答。

How can I read a data, in a txt file, as a matrix?

1 次查看(过去 30 天)
I am working on a deep learning project and for the first step, I need to read a txt file as a matrix so that I can reach every features easily.
I have 90 classes and 42 features for each of them:
{feature1, feature2, ..., feature42}
{feature1, feature2, ..., feature42}
......
{feature1, feature2, ..., feature42}
The features starts after the keyword '@DATA' in the txt file
I can read the first row with this one. Since the keyword '@DATA' starts at line 47, I am reading the line 48. For sure, I also need to read the other lines until the end of the file. However, before that, I tried to see whether I can read the first row correctly or not.
% open the file
fid=fopen('BlindAllFeatures.txt');
% set linenum to the desired line number that you want to import
linenum = 48;
% use '%s' if you want to read in the entire line or use '%f' if you want to read only the first numeric value
C = textscan(fid,'%s',1,'delimiter','\n', 'headerlines',linenum-1);
But I am getting a 1x1 cell that has the whole {feature1, feature2, ..., feature42} when I run this code. You can see it from the image I uploaded. I need 90 rows and 42 columns but I couldn't do that. If I can do that, I will try to train them by trying different classifiers and after that, I will move on to feature selection part. I hope you can help me. Thank you.

回答(1 个)

Maria
Maria 2020-7-29
Try using strplit on the C array that you got.
C = strsplit(C,', ')
  2 个评论
Sezin Koktas
Sezin Koktas 2020-7-29
It says:
Error using strsplit (line 80)
First input must be either a character vector or a string scalar.
Error in Feature (line 57)
C = strsplit(C,', ')
Sezin Koktas
Sezin Koktas 2020-7-29
Now it is giving this,
Error using strsplit (line 91)
The delimiter must be either a character vector, a cell array of character vectors, or a string array.
Error in Feature (line 57)
C = strsplit(',',C);

此问题已关闭。

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by