How to read different data types from a txt file

5 views (last 30 days)
Hi all, How can I read this file and assign each line to a variable
USB
00000001
8
10000000111
128
0.0000008

Accepted Answer

Guillaume
Guillaume on 13 Dec 2019
lines = strsplit(fileread('C:\somewhere\yourfile'), {'\r', '\n'});
lines will be a cell array with each cell containing a line of your text file.
  2 Comments
Guillaume
Guillaume on 13 Dec 2019
You're going to have problems reading 80e-9 as integer!
Anyway
str2double(lines(2:end))
will convert all but the first line to real numbers.
Omar Ashraf
Omar Ashraf on 16 Dec 2019
Edited: Omar Ashraf on 16 Dec 2019
Thanks a lot, I really appreciate your help <3
That's what worked for me:
lines= strsplit(fileread('USB_Config.txt'),{'\r', '\n'});
A=lines(1);
B=str2num((lines{2}.'));%Binary
C=lines(3);
D=str2num((lines{4}.'));%Binary
E=lines(5);
F=lines(6);

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!