同じ形式のファイルを​読み込もうとするとエ​ラーが出ます.

9 views (last 30 days)
拳志朗
拳志朗 on 30 Nov 2023
Commented: 拳志朗 on 1 Dec 2023
Fname='AIC010.txt';%データの読み込み
t=readtable(Fname,...
"NumHeaderLines",3,"ReadVariableNames",false);%ヘッダーの行数
t(:,6:end)=[];%不要な部分(Soil Column)を削除
t.Properties.VariableNames={'Depth[m]', 'N-Value', 'P-Velocity[m/s]',...
'S-Velocity[m/s]', 'Density[Mg/m^3]'};%ヘッダーの名前を決定
t.("Depth[m]")=str2double(erase(t.("Depth[m]"), 'm'));%深さを数値とする
disp(t)%表示する
次を使用中のエラー: .
VariableNames プロパティにはテーブル内の変数ごとに名前を 1 つ指定しなければなりません。
エラー: untitled2 (行 7)
t.Properties.VariableNames={'Depth[m]', 'N-Value', 'P-Velocity[m/s]',...
AIC001で実行した場合は以下のようになります.
Depth[m] N-Value P-Velocity[m/s] S-Velocity[m/s] Density[Mg/m^3]
________ _______ _______________ _______________ _______________
1 2 360 78 1.8
2 13 360 78 1.68
3 14 360 130 1.72
4 4 360 130 1.71
5 2 1600 130 1.68
6 5 1600 130 1.71
7 13 1600 130 1.88
8 6 1600 130 1.89
9 7 1600 130 1.9
10 22 1600 130 1.9
11 27 1600 200 1.89
12 24 1600 200 1.87
13 17 1600 200 1.87
14 12 1600 200 1.86
15 11 1600 200 1.85
16 20 1600 200 1.89
17 29 1600 200 1.9
18 26 1600 200 1.92
19 24 1600 200 1.82
20 26 1600 200 1.85
AIC010の時にエラーが出るのかわかりません.

Accepted Answer

Akira Agata
Akira Agata on 30 Nov 2023
もしかすると 2 つのファイルの Delimiter の微妙な違いが影響している可能性があります (いずれも単純なタブ区切りではなく、半角スペースの連続で区切っているようです)。試したところ、readtable のオプションをより詳細に指定することで対応できました。
% 対象データ
% AIC010 (AIC001の場合は2番目のURLを使用)
url = "https://jp.mathworks.com/matlabcentral/answers/uploaded_files/1556142/AIC010%20.txt";
% url = "https://jp.mathworks.com/matlabcentral/answers/uploaded_files/1556147/AIC001.txt";
% データ部分を読み込み
t1 = readtable(url,...
"NumHeaderLines", 3,...
"ReadVariableNames", false,...
"Delimiter"," ",... % Delimiterが半角スペースであることを明示的に指定
"ConsecutiveDelimitersRule", "join"); % 半角スペースの連続は1つのDelimiterとして扱う
% 不要な列を削除
t1(:,[1 7:end]) = [];
% ヘッダーの名前を決定
t1.Properties.VariableNames = {'Depth[m]', 'N-Value', 'P-Velocity[m/s]',...
'S-Velocity[m/s]', 'Density[Mg/m^3]'};
% 深さを数値とする
t1.("Depth[m]") = str2double(erase(t1.("Depth[m]"), 'm'));
% 表示する
disp(t1)
Depth[m] N-Value P-Velocity[m/s] S-Velocity[m/s] Density[Mg/m^3] ________ _______ _______________ _______________ _______________ 1 13 560 220 1.82 2 3 1540 130 1.82 3 8 1560 180 1.76 4 5 1540 160 1.85 5 6 1590 170 1.89 6 9 1610 190 1.82 7 12 1610 210 1.81 8 19 1670 250 1.86 9 28 1700 280 1.94 10 34 1720 300 1.84 11 50 1750 350 1.92 12 50 1750 350 1.97 13 50 1750 350 1.96 14 32 1700 300 1.96 15 48 1790 340 1.95 16 50 1850 440 1.96 17 50 1820 470 1.96 18 50 1920 490 1.98 19 50 1890 510 1.97 20 50 1920 520 1.97
  1 Comment
拳志朗
拳志朗 on 1 Dec 2023
半角スペースの連続数がテキストファイルによって違ったんですね.
理解しました.
おかげさまで問題が解決できました.
本当にありがとうございます.
また,見かけた際はよろしくお願いします.

Sign in to comment.

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!