MATLAB Answers

Is it possible to write an 'empty' cell in Matlab, not NaN?

1 view (last 30 days)
I would like to write an empty cell in an output file which I will save as a .bty file.
The attached file is an example. In the second column, first row, the value is NaN.
In Excel, I can open the same file, delete the 0 or NaN, and save the file, and this cell will appear empty. How do I do this in Matlab?
  5 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 3 Aug 2021
In the below code, I examine the first numeric line, and I assume that if the second element is nan, that the first element is intended to be the point count. In such a case, I drop that first line, leaving the array without a point count.
I calculate the size of the numeric array ( after the leading line might have been dropped) and I write that in as a header.
The reason that I do not just rely upon the first line as being the point count is that you have a mismatch: the header in your example file says 99, but there are then 100 lines of data after it. As the file format required that the header be the count, we must ensure that the count is accurate, even if that means ignoring the input count.
If the only possible nan was on that first line, then some of this code is not necessary. This code handles the case where there might be additional nan (though that would not seem valid according to the .pdf file.)
input_filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/702192/example.csv';
output_filename = 'example.bty';
want_piecewise_linear_fit = true;
YourArray = readmatrix(input_filename);
if isnan(YourArray(1,2))
YourArray = YourArray(2:end,:);
end
mask = isnan(YourArray);
tempcell = num2cell(YourArray);
tempcell(mask) = {[]};
headercell = cell(2, size(YourArray,2));
if want_piecewise_linear_fit
headercell{1,1} = 'L';
else
headercell{1,1} = 'C';
end
headercell{2,1} = size(YourArray,1);
cell_to_write = [headercell; tempcell];
writecell(cell_to_write, output_filename, 'Filetype', 'text', 'delimiter', 'space');
type(output_filename)
L 100 0 11.741 0.010101 11.741 0.020202 11.678 0.030303 11.872 0.040404 11.789 0.050505 11.789 0.060606 11.7 0.070707 11.7 0.080808 11.602 0.090909 11.602 0.10101 11.483 0.11111 11.483 0.12121 11.483 0.13131 11.332 0.14141 11.332 0.15152 11.153 0.16162 11.153 0.17172 11.106 0.18182 11.106 0.19192 10.948 0.20202 10.948 0.21212 10.795 0.22222 10.795 0.23232 10.588 0.24242 10.588 0.25253 10.588 0.26263 10.238 0.27273 10.238 0.28283 9.6187 0.29293 9.6187 0.30303 8.5101 0.31313 9.0808 0.32323 7.4422 0.33333 7.4422 0.34343 5.4709 0.35354 5.4709 0.36364 3.3529 0.37374 3.3529 0.38384 3.3529 0.39394 1.2394 0.40404 1.2394 0.41414 0.70178 0.42424 0.70178 0.43434 2.2325 0.44444 2.2325 0.45455 2.9955 0.46465 2.9955 0.47475 3.2846 0.48485 3.2846 0.49495 3.4146 0.50505 3.4146 0.51515 3.4146 0.52525 3.466 0.53535 3.466 0.54545 3.4676 0.55556 3.4676 0.56566 3.4394 0.57576 3.4394 0.58586 3.4085 0.59596 3.3791 0.60606 3.3777 0.61616 3.3777 0.62626 3.374 0.63636 3.374 0.64646 3.3395 0.65657 3.3395 0.66667 3.3395 0.67677 3.274 0.68687 3.274 0.69697 3.2051 0.70707 3.2051 0.71717 3.1634 0.72727 3.1634 0.73737 3.1645 0.74747 3.1645 0.75758 3.208 0.76768 3.208 0.77778 3.2321 0.78788 3.2321 0.79798 3.2321 0.80808 3.1795 0.81818 3.1795 0.82828 2.9657 0.83838 2.9657 0.84848 2.4782 0.85859 2.4782 0.86869 1.6777 0.87879 2.1615 0.88889 0.97668 0.89899 0.97668 0.90909 0.57579 0.91919 0.57579 0.92929 0.57579 0.93939 2.3425 0.94949 2.3425 0.9596 4.1989 0.9697 4.1989 0.9798 6.072 0.9899 6.072 1 7.9096
  2 Comments
Walter Roberson
Walter Roberson on 4 Aug 2021
If you look at section 6.1, then the count in the example file is 5, and there are 5 lines after that.
If you are intending for distances relative to something, then that could potentially imply that after you read the array, you should be subtracting the first row (that does not have a nan) from the other rows, and that the difference should be written. If so then that would not be a difficult change to make.

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by