Is it possible to write an 'empty' cell in Matlab, not NaN?
34 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Louise Wilson
el 3 de Ag. de 2021
Comentada: Walter Roberson
el 4 de Ag. de 2021
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 comentarios
Walter Roberson
el 3 de Ag. de 2021
Is it the file format as described in https://oalib-acoustics.org/AcousticsToolbox/Bellhop-2010-1.pdf section 6.1 ?
Respuesta aceptada
Walter Roberson
el 3 de Ag. de 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)
2 comentarios
Walter Roberson
el 4 de Ag. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Variables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!