Pulling certain data from a text file into Matlab with strsplit

1 view (last 30 days)
Hi, I want a function that reads this text file. It would be better if this could be read with fgetl and strsplit.Thanks!

Answers (1)

Star Strider
Star Strider on 25 Jan 2022
Try something like this —
% opts = weboptions('ContentType','text');
% M = webread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/873530/miktos1.txt', opts)
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/873530/miktos1.txt', 'ReadVariableNames',0)
T1 = 8×2 table
Var1 Var2 ___________________ _______________________________________________ {'LOADS' } {'20 40 kN/m' } {'ELASTICITY' } {'28e6 210e6 kPa concrete/steel'} {'SPECIFIC WEIGHT'} {'25 78.6 kN/m3 concrete/steel'} {'MAX STRESS' } {'20e3 355e3 kPa concrete/steel'} {'COST' } {'100 8000 Euros/m3 concrete/steel'} {'MAX DEFLECTION' } {'0.05 m' } {'SPANS' } {'4 4 2 2 4 4 m' } {'HEIGHTS' } {'1 3 m' }
for k = 1:size(T1,1)
Var2stsp{k,:} = strsplit(T1{k,2}{:},' ');
end
Var2stsp{1}
ans = 1×3 cell array
{'20'} {'40'} {'kN/m'}
Var2stsp{2}
ans = 1×4 cell array
{'28e6'} {'210e6'} {'kPa'} {'concrete/steel'}
Var2stsp{end-1}
ans = 1×7 cell array
{'4'} {'4'} {'2'} {'2'} {'4'} {'4'} {'m'}
Var2stsp{end}
ans = 1×3 cell array
{'1'} {'3'} {'m'}
Experiment to get the desired result.
.

Categories

Find more on Just for fun in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!