How to fix " --> " arrow like Data as result of read file function?
20 次查看(过去 30 天)
显示 更早的评论
Hi,Community
this kinda new to me, because i rarely found this data as an output of read file function, like readmatrix, readtable, etc.... Iam just curious about how to handle these data that re annoyed me :
for emd_files = 1:emd_x
data_emd1 = detectImportOptions(fulls_emd{emd_files}, 'ReadVariableNames',false, 'Delimiter', ' ',...
'ThousandsSeparator', '', 'ConsecutiveDelimitersRule', 'join', 'FileType', 'delimitedtext', 'EmptyLineRule', 'skip', 'LeadingDelimitersRule', 'ignore');
data_emd1.VariableNames = ["Var1", "Var2", "Var3", "Var4", "Var5", "Var6", "Var7", "Var8", "Var9", "Var10", "Var11", "Var12", "Var13", "Var14", "Var15", "Var16"];
data_emd1.VariableTypes = ["string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string"];
tabel_file_fixeds{emd_files} = readtable(fulls_emd{emd_files}, data_emd1);
k_array = str2double(table2array(tabel_file_fixeds{emd_files}(:, 1:8)));
a_array = str2double(table2array(tabel_file_fixeds{emd_files}(:, 9:16)));
end
when i execute tabel_file_fixeds{emd_files}, it show me an " error like" data like this :
"4" "3" "3" "3" "2" "2" "2" "1" "27" "15" "15" "15" "7" "7" "7" "3"
"4" "3" "4" "3" "2" "4" "4" "4" "27" "15" "27" "15" "7" "27" "27" "27"
"7" "7" "7" "7" "7" "6" "5" "4→140→140→140→140→140" "80" "48" "27" <missing> <missing> <missing> <missing> <missing>
"5" "4" "4" "3" "4" "5" "4" "4" "48" "27" "27" "15" "27" "48" "27" "27"
"5" "5" "4" "3" "3" "3" "3" "3" "48" "48" "27" "15" "15" "15" "15" "15"
I cannot fix the data in row 3 above that re actually is :
"7" "7" "7" "7" "7" "6" "5" "4" "80" "48" "27" "140" "140" "140" "140" "140"
Every row in the extracted data should be a 1 x 16 Data like the above.... But why the arrow ( -> ) data suddenly appears like that when handling 3 digits (hundred) or maybe more digit data (thousand, ten thousand, etc) , such as 140, 1400, etc....
Then, if the k_array data was executed by using that arrow data, it would become a NaN data... That so weird... Because i need these 140 data to be calculated...
Iam very grateful if someone can give me a solution to handle my problem here.... Thank you so much, everyone...
4 个评论
Star Strider
2022-4-25
Using readmatrix produces:
M1 = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/976855/5K_Indeks_TUN.txt')
Is this the result you want?
.
采纳的回答
Voss
2022-4-25
编辑:Voss
2022-4-25
Those arrows represent tabs. In the file, the numbers are separated by a tab followed by one or two spaces, except before the 140's, which have no spaces, just tabs. Presumably that's because they're three-digit numbers and whatever program created this file used a tab followed by however many spaces are necessary to get the numbers to right-align.
When you read the file with readtable, you use space as the only delimiter (because that's what detectImportOptions tells you is the delimiter, perhaps). Using space as the delimiter when there are no spaces between numbers (as in the 140 140 140 ... sequence), causes those to run together. So instead use tab as the delimiter, or use tab and space both (since 'ConsecutiveDelimitersRule' is 'join'):
fulls_emd = {'5K_Indeks_TUN.txt'};
emd_files = 1;
data_emd1 = detectImportOptions(fulls_emd{emd_files}, ...
'ReadVariableNames',false, ...
'Delimiter', '\t',... % or {' ','\t'} (space and tab)
'ThousandsSeparator', '', ...
'ConsecutiveDelimitersRule', 'join', ...
'FileType', 'delimitedtext', ...
'EmptyLineRule', 'skip', ...
'LeadingDelimitersRule', 'ignore');
data_emd1.VariableNames = ["Var1", "Var2", "Var3", "Var4", "Var5", "Var6", "Var7", "Var8", "Var9", "Var10", "Var11", "Var12", "Var13", "Var14", "Var15", "Var16"];
data_emd1.VariableTypes = ["string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string"];
tabel_file_fixeds{emd_files} = readtable(fulls_emd{emd_files}, data_emd1);
disp(tabel_file_fixeds{emd_files})
k_array = str2double(table2array(tabel_file_fixeds{emd_files}(:, 1:8)));
a_array = str2double(table2array(tabel_file_fixeds{emd_files}(:, 9:16)));
disp(k_array(end-6:end,:))
disp(a_array(end-6:end,:))
EDIT: or use readmatrix:
M = readmatrix(fulls_emd{emd_files});
k_array_new = M(:, 1:8);
a_array_new = M(:, 9:16);
isequal(k_array_new,k_array)
isequal(a_array_new,a_array)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!