Info

This question is closed. Reopen it to edit or answer.

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

1 view (last 30 days)
Sezin Koktas
Sezin Koktas on 29 Jul 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
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.

Answers (1)

Maria
Maria on 29 Jul 2020
Try using strplit on the C array that you got.
C = strsplit(C,', ')
  2 Comments
Sezin Koktas
Sezin Koktas on 29 Jul 2020
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 on 29 Jul 2020
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);

This question is closed.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!