Pulling certain data from a text file into Matlab with strsplit

1 回表示 (過去 30 日間)
Alexandros Gakos
Alexandros Gakos 2022 年 1 月 25 日
回答済み: Star Strider 2022 年 1 月 25 日
Hi, I want a function that reads this text file. It would be better if this could be read with fgetl and strsplit.Thanks!

回答 (1 件)

Star Strider
Star Strider 2022 年 1 月 25 日
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.
.

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by