How to extract text and value from a .txt file ?

Hi,
i have to import an array from a .txt file into Matlab. The problem is I have 2 arrays of similar shape that contain text and numbers (I would like both).
I tried using "uiimport" (which is convenient since I'm making a GUI): It works well with the file1 : I get a "data" matrix (I don't know what it is) and a "textdata" cell array which I can work with.
However here is the problem, with the file2, the "textdata" cell array only contains the first line.
Could you please help me find a solution ?
(I suggest the problem may be related to the last column being empty)

7 Comments

Hello Karim, if you put your text file here it can be easier to answer your question. But you can use the textscan command to read your file.
The first input is file identifier: fid
The second input is the FormatSpec: Numbers=%f, Strings=%q, etc.
You should define your formatSpec in a way that is arranged in your text file. There are many options regarding textscan command, for example which lines to not read, or how many lines to read and all of them depend on the arrangement of your text file.
You can use the following code as an example to have an idea:
fid = fopen('YourFile.txt')
data = textscan(fid,'%f%q')
fclose(fid)
Hello thank you for your answer, the files are attached to my first message, I'll add them to this one too. Somebody gave me this solution but it doesn't work with file2.
fid = fopen('Tableau 1.txt', 'r');
X = textscan(fid, '%s%s%s%s%s%s%s%d%d%d%d%d%d%f%f%f%f%f%f%f%s%d', 'delimiter', '\t', 'headerlines', 1);
fclose(fid);
"Somebody gave me this solution but it doesn't work with file2."
The two files are both tab-separated, but that is where the similarities end: they have different numbers of columns, different column headers, and different numeric/string types in the different columns.
It is not clear why you expected that textscan call to work with both files.
For the file2.txt I tried to arrange your data (except the header line) in the variable b. Try the code!
Then you can see which column is interesting for you and choose it.
clc
clear all
close all
fid = fopen('file2.txt')
k = 0;
data = [];
a = [];
fgetl(fid)
while ~feof(fid)
k = k+1;
data{k} = strsplit(fgetl(fid))
a = cell2table(data{k});
b(k,:) = a{1,1:30};
end
fclose(fid)
@Kian, use the answer box for answers, rather than comments on the question.
That way, you'll get reputation points if Karim accepts your answer. He can't do that with comments.
@Guillaume thank you for your comment. I didn't know it. For the next times I will answer the questions instead of put a comment.
Thank you for your help, this helped me to resolve my problem.

Sign in to comment.

 Accepted Answer

aL
aL on 29 Sep 2017
You can use the readtable command.
filedata = readtable('file2.txt','delimiter','\t','readvariablenames',true);

More Answers (0)

Asked:

on 29 Sep 2017

Commented:

on 3 Oct 2017

Community Treasure Hunt

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

Start Hunting!