How to load numeric & string data from .txt file
48 views (last 30 days)
Show older comments
Dear all,
It might be a regular and common topic but I am unable to fix this issue properly.
Starting from a .txt file (let's say data.txt):
data_name1 data_name2 data_name3
0 OK 0,1,2
1 NOK 0,2.05E-05,8.5
Goal is to have in matlab workspace:
data_name1 = [ 0 ; 1 ]
data_name2 = {'OK','NOK'}
data_name3 = [ 0 1 2 ; 0 2.05E-05 8.5]
I tried readtext.m from file exchange but take huge amount of time and need lots of specific treatment afterward. Using eval also increased processing time of my function. Note: I have no specific toolbox apart from simulink.
Thanks in advance,
Pierre
1 Comment
Luca Parziale
on 30 Jun 2022
Hi, maybe my code can help you. It's not better than Matt Kindig but it may inspire you :
% load .txt data into string vector
PC = readlines("pouvoir_calorifique_brute_moyen.txt");
PC = split(PC); % separate at each space to create an string array with column
PC = replace(PC,"_"," ") % now change the multi-words names
labelx = PC(1,2);
labely = PC(1,3);
l = length(PC);
PC_kg = PC(2:l, 2); PC_kg = str2double(PC_kg);
PC_m3 = PC(2:l, 3); PC_m3 = str2double(PC_m3);
labels = PC(2:l, 1);
Accepted Answer
Matt Kindig
on 17 Oct 2012
Edited: Matt Kindig
on 17 Oct 2012
This should do it, for your specific case:
fid = fopen('data.txt', 'rt');
C = textscan(fid, '%d %s %f,%f,%f', 'HeaderLines', 1, 'CollectOutput', true);
fclose(fid);
data_name1 = C{1};
data_name2 = C{2};
data_name3 = C{3};
3 Comments
Matt Kindig
on 17 Oct 2012
Edited: Matt Kindig
on 17 Oct 2012
Hmm, so that makes it substantially trickier. How large is the file? If you can read it into Matlab all at once, you might be able to use regular expressions to do this. I don't have time right now to code up a full solution, but this sample code should get you started.
EDIT: Found a better way, updated code to minimize eval() statements.
str = fileread('data.txt'); %read entire file into string
parts = strtrim(regexp( str, '(\r|\n)+', 'split')); %split by each line
columns = strtrim( regexp(parts{1}, '\s+', 'split')); %columns
ncol = length(columns); %number of columns
parts(1)= []; %remove column headers
nrows = length(parts); %number of rows
M = cell( nrows, ncol); %pre-allocate empty cell array for data
%now loop through parts
for k=1:nrows,
data = strtrim(regexp( parts{k}, '\s+', 'split')); %split by spaces
M(k,:) = data;
end
%now assign variables
for k=1:ncol,
row = M(:,k);
eval( sprintf('%s=row', columns{k}));
end
Keep in mind that this will regard all text as cell arrays of characters. To convert to numeric arrays, you will have to do some more work-- one of the difficulties is how to distinguish between pure text (e.g. 'OK'), and numeric data with chars in between (like the comma delimited list in data_name3).
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!