replace characters (like 'n/a') with 0 inside a .txt text file

1 次查看(过去 30 天)
Hi! I have a txt file like the one attached. Inside there are numbers and sometimes 'n/a' characters. How can I replace 'n/a' with 0? Because I tried to read the file but Matlab gives the following error:
folder = '....';
file = 'test_data.txt';
FILE_import = load(fullfile(folder,file));
% ====================================
Error using load
Unknown text on line number 1 of ASCII file C:\Users\Alberto\Desktop\test_data.txt
"n/a".

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-10-2
编辑:Dyuman Joshi 2023-10-2
To read the data from a file, use readmatrix (or readtable) instead of load.
The 'n/a' characters will be read as NaN. Then replace the NaN values with 0.
y = readmatrix('test_data.txt');
disp(y)
Columns 1 through 19 28.0445 -17.2717 83.9720 -0.3225 0.4179 -0.8494 0.1025 -0.8766 -0.4702 -0.9410 -0.2387 0.2398 3.1647 1.9182 4.5658 NaN 2.5589 0.5604 9.7606 27.8199 -16.9860 83.3748 -0.3199 0.3993 -0.8592 0.1350 -0.8783 -0.4586 -0.9378 -0.2627 0.2271 3.7023 2.2641 5.4424 0.0309 3.4615 0.6360 12.2287 27.5723 -16.6835 82.7018 -0.3162 0.3777 -0.8703 0.1688 -0.8803 -0.4434 -0.9336 -0.2871 0.2146 3.7534 2.5486 5.6091 0.0325 3.5880 0.6397 12.1458 27.2756 -16.3390 81.8710 -0.3100 0.3493 -0.8842 0.2179 -0.8792 -0.4237 -0.9254 -0.3240 0.1964 3.4905 2.8198 4.6453 0.0356 3.4305 0.7385 11.0657 26.9530 -15.9879 80.9281 -0.3000 0.3148 -0.9005 0.2857 -0.8710 -0.3996 -0.9102 -0.3771 0.1713 3.3481 3.0842 3.6854 0.0391 3.3020 0.8960 10.5495 26.6278 -15.6599 79.9185 -0.2848 0.2757 -0.9181 0.3768 -0.8485 -0.3717 -0.8814 -0.4518 0.1378 3.3049 3.0150 3.5599 0.0429 3.2895 0.9240 10.4580 26.3208 -15.3744 78.8818 -0.2630 0.2347 -0.9358 0.4968 -0.7985 -0.3399 -0.8271 -0.5543 0.0934 3.3111 2.9568 3.6604 0.0461 3.2692 0.8931 10.4078 26.0484 -15.1390 77.8484 -0.2334 0.1960 -0.9524 0.6452 -0.7016 -0.3025 -0.7275 -0.6851 0.0373 3.3326 2.8585 3.7989 0.0484 3.2989 0.8684 10.5351 25.8214 -14.9500 76.8370 -0.1959 0.1640 -0.9668 0.8037 -0.5380 -0.2542 -0.5619 -0.8268 -0.0264 3.3836 2.8781 3.7909 0.0496 3.3537 0.8847 10.6689 25.6449 -14.7961 75.8546 -0.1524 0.1425 -0.9780 0.9287 -0.3179 -0.1911 -0.3381 -0.9374 -0.0839 3.3565 2.9591 3.6423 0.0498 3.3154 0.9103 10.5811 25.5186 -14.6631 74.8983 -0.1067 0.1323 -0.9855 0.9866 -0.1090 -0.1214 -0.1235 -0.9852 -0.1189 3.4010 3.1099 3.5661 0.0483 3.3614 0.9426 10.6764 25.4379 -14.5388 73.9580 -0.0639 0.1297 -0.9895 0.9979 -0.0015 -0.0646 -0.0099 -0.9915 -0.1294 3.4240 3.2709 3.7352 0.0424 3.3886 0.9072 10.8032 25.3948 -14.4165 73.0209 -0.0289 0.1283 -0.9913 0.9948 -0.0935 -0.0411 -0.0979 -0.9873 -0.1249 3.5157 3.2379 3.9587 0.0319 3.4745 0.8777 11.2161 25.3798 -14.2969 72.0744 -0.0046 0.1211 -0.9926 0.8424 -0.5344 -0.0690 -0.5388 -0.8365 -0.0995 3.6345 3.2076 4.1639 0.0228 3.5544 0.8536 11.6176 25.3826 -14.1869 71.1100 0.0085 0.1037 -0.9946 0.3102 -0.9458 -0.0960 -0.9506 -0.3077 -0.0402 3.5687 2.9947 3.9218 0.0249 3.5020 0.8929 11.2550 25.3932 -14.0973 70.1247 0.0113 0.0759 -0.9971 -0.0603 -0.9953 -0.0764 -0.9981 0.0610 -0.0067 3.4767 2.7000 4.0740 0.0329 3.3966 0.8337 10.9775 25.4023 -14.0384 69.1215 0.0055 0.0405 -0.9992 -0.2491 -0.9676 -0.0406 -0.9685 0.2491 0.0048 5.2573 2.6349 11.2915 0.0389 3.8595 0.3418 20.9664 25.4015 -14.0163 68.1081 -0.0083 0.0035 -1.0000 -0.4430 -0.8965 0.0005 -0.8965 0.4430 0.0090 5.1355 2.9369 9.9821 0.0393 4.2907 0.4298 19.3923 25.3805 -14.0329 67.0190 -0.0318 -0.0335 -0.9989 -0.6180 -0.7848 0.0460 -0.7856 0.6188 0.0043 5.3555 2.9336 9.3084 NaN 4.7505 0.5103 18.6899 Columns 20 through 21 6.2440 0.2953 10.5823 0.5999 10.8946 0.6634 9.4903 0.5107 8.7088 0.3795 8.6004 0.1906 8.5063 0.1727 8.6884 0.2968 8.9451 0.2426 8.7702 0.1438 8.9719 0.2105 9.1518 0.3675 9.7426 0.4313 10.3233 0.4703 9.8536 0.4637 9.3216 0.4914 20.2297 0.8860 20.8015 0.8718 22.1968 0.8188
%Check how many NaN elements there are in the array
nnz(isnan(y))
ans = 2
%Replace NaN with 0
y(isnan(y)) = 0;
disp(y)
Columns 1 through 19 28.0445 -17.2717 83.9720 -0.3225 0.4179 -0.8494 0.1025 -0.8766 -0.4702 -0.9410 -0.2387 0.2398 3.1647 1.9182 4.5658 0 2.5589 0.5604 9.7606 27.8199 -16.9860 83.3748 -0.3199 0.3993 -0.8592 0.1350 -0.8783 -0.4586 -0.9378 -0.2627 0.2271 3.7023 2.2641 5.4424 0.0309 3.4615 0.6360 12.2287 27.5723 -16.6835 82.7018 -0.3162 0.3777 -0.8703 0.1688 -0.8803 -0.4434 -0.9336 -0.2871 0.2146 3.7534 2.5486 5.6091 0.0325 3.5880 0.6397 12.1458 27.2756 -16.3390 81.8710 -0.3100 0.3493 -0.8842 0.2179 -0.8792 -0.4237 -0.9254 -0.3240 0.1964 3.4905 2.8198 4.6453 0.0356 3.4305 0.7385 11.0657 26.9530 -15.9879 80.9281 -0.3000 0.3148 -0.9005 0.2857 -0.8710 -0.3996 -0.9102 -0.3771 0.1713 3.3481 3.0842 3.6854 0.0391 3.3020 0.8960 10.5495 26.6278 -15.6599 79.9185 -0.2848 0.2757 -0.9181 0.3768 -0.8485 -0.3717 -0.8814 -0.4518 0.1378 3.3049 3.0150 3.5599 0.0429 3.2895 0.9240 10.4580 26.3208 -15.3744 78.8818 -0.2630 0.2347 -0.9358 0.4968 -0.7985 -0.3399 -0.8271 -0.5543 0.0934 3.3111 2.9568 3.6604 0.0461 3.2692 0.8931 10.4078 26.0484 -15.1390 77.8484 -0.2334 0.1960 -0.9524 0.6452 -0.7016 -0.3025 -0.7275 -0.6851 0.0373 3.3326 2.8585 3.7989 0.0484 3.2989 0.8684 10.5351 25.8214 -14.9500 76.8370 -0.1959 0.1640 -0.9668 0.8037 -0.5380 -0.2542 -0.5619 -0.8268 -0.0264 3.3836 2.8781 3.7909 0.0496 3.3537 0.8847 10.6689 25.6449 -14.7961 75.8546 -0.1524 0.1425 -0.9780 0.9287 -0.3179 -0.1911 -0.3381 -0.9374 -0.0839 3.3565 2.9591 3.6423 0.0498 3.3154 0.9103 10.5811 25.5186 -14.6631 74.8983 -0.1067 0.1323 -0.9855 0.9866 -0.1090 -0.1214 -0.1235 -0.9852 -0.1189 3.4010 3.1099 3.5661 0.0483 3.3614 0.9426 10.6764 25.4379 -14.5388 73.9580 -0.0639 0.1297 -0.9895 0.9979 -0.0015 -0.0646 -0.0099 -0.9915 -0.1294 3.4240 3.2709 3.7352 0.0424 3.3886 0.9072 10.8032 25.3948 -14.4165 73.0209 -0.0289 0.1283 -0.9913 0.9948 -0.0935 -0.0411 -0.0979 -0.9873 -0.1249 3.5157 3.2379 3.9587 0.0319 3.4745 0.8777 11.2161 25.3798 -14.2969 72.0744 -0.0046 0.1211 -0.9926 0.8424 -0.5344 -0.0690 -0.5388 -0.8365 -0.0995 3.6345 3.2076 4.1639 0.0228 3.5544 0.8536 11.6176 25.3826 -14.1869 71.1100 0.0085 0.1037 -0.9946 0.3102 -0.9458 -0.0960 -0.9506 -0.3077 -0.0402 3.5687 2.9947 3.9218 0.0249 3.5020 0.8929 11.2550 25.3932 -14.0973 70.1247 0.0113 0.0759 -0.9971 -0.0603 -0.9953 -0.0764 -0.9981 0.0610 -0.0067 3.4767 2.7000 4.0740 0.0329 3.3966 0.8337 10.9775 25.4023 -14.0384 69.1215 0.0055 0.0405 -0.9992 -0.2491 -0.9676 -0.0406 -0.9685 0.2491 0.0048 5.2573 2.6349 11.2915 0.0389 3.8595 0.3418 20.9664 25.4015 -14.0163 68.1081 -0.0083 0.0035 -1.0000 -0.4430 -0.8965 0.0005 -0.8965 0.4430 0.0090 5.1355 2.9369 9.9821 0.0393 4.2907 0.4298 19.3923 25.3805 -14.0329 67.0190 -0.0318 -0.0335 -0.9989 -0.6180 -0.7848 0.0460 -0.7856 0.6188 0.0043 5.3555 2.9336 9.3084 0 4.7505 0.5103 18.6899 Columns 20 through 21 6.2440 0.2953 10.5823 0.5999 10.8946 0.6634 9.4903 0.5107 8.7088 0.3795 8.6004 0.1906 8.5063 0.1727 8.6884 0.2968 8.9451 0.2426 8.7702 0.1438 8.9719 0.2105 9.1518 0.3675 9.7426 0.4313 10.3233 0.4703 9.8536 0.4637 9.3216 0.4914 20.2297 0.8860 20.8015 0.8718 22.1968 0.8188
Edit - Fixed some typos.
  2 个评论
Alberto Acri
Alberto Acri 2023-10-2
Thanks for your solution. With readmatrix it is possible to read the file from a folder other than pwd?
Dyuman Joshi
Dyuman Joshi 2023-10-2
编辑:Dyuman Joshi 2023-10-2
You can read the file from any folder, granted MATLAB has permission to access the folder and the file.
If the file is not in the current folder or in a folder on the MATLAB path, then specify the full path of the file -
%Example
readmatrix('C:\myFolder\myFile.xlsx')

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by