How to load numeric & string data from .txt file
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
More Answers (0)
Categories
Find more on Environment and Settings 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!