Insert blank cells in a matrix

How can I replace all the NaN values of a matrix with a blank space?

 Accepted Answer

You cannot do that. Numeric matrices cannot have spaces in them.
You can convert the matrix into a cell array and put spaces into the cell array... but it is not clear why you would do that.
myCell = num2cell(MyMatrix);
myCell(isnan(MyMatrix)) = {' '}; %nan -> space
Now what?

12 Comments

Emilio Pulli
Emilio Pulli on 30 Nov 2021
Edited: Emilio Pulli on 30 Nov 2021
Because I have a huge matrix which is composed of blocks of numbers (sub-matrices) which are separated by NaN rows. This huge matrix needs to be converted in a txt files where the rows with NaN values must be converted into delimiting blank spaces
Anyway, your code doesn't work...
MyMatrix = rand(4,4);
MyMatrix(randi(16,1,2)) = nan
MyMatrix = 4×4
0.9622 0.3137 0.0406 0.5866 0.3809 0.8690 NaN 0.3173 0.7191 0.4954 NaN 0.2221 0.1190 0.2682 0.8609 0.7682
myCell = num2cell(MyMatrix);
myCell(isnan(MyMatrix)) = {' '}; %nan -> space
myCell
myCell = 4×4 cell array
{[0.9622]} {[0.3137]} {[0.0406]} {[0.5866]} {[0.3809]} {[0.8690]} {' ' } {[0.3173]} {[0.7191]} {[0.4954]} {' ' } {[0.2221]} {[0.1190]} {[0.2682]} {[0.8609]} {[0.7682]}
Looks to me as if it works.
This huge matrix needs to be converted in a txt files where the rows with NaN values must be converted into delimiting blank spaces
Do I understand correctly that you have rows that are all-NaN and that those must be converted to empty rows ?
How are you doing the writing at present?
How many digits of precision do you need when writing out the data?
Emilio Pulli
Emilio Pulli on 30 Nov 2021
Edited: Emilio Pulli on 30 Nov 2021
Yes, exactly you got the point. At the moment, I simply write a txt file using the matrix with the NaN rows. Maybe, I have to convert the matrix with the NaN rows into the txt file and after I need a piece of code that enter the txt file and modifies it by replacing the NaN rows with blank spaces
Walter Roberson
Walter Roberson on 30 Nov 2021
Edited: Walter Roberson on 30 Nov 2021
What commands are you currently using to write the matrix to the file?
At the time that the matrix is to be written to the file, is there still information hanging around about where the NaN rows are? Perhaps they are at regular intervals? Perhaps you happened to already locate the locations in order to write the NaN into there in the first place?
The NaN are each 100 rows...therefore I have NaN rows at row number 101, 201, 301...At a certain moment of the matrix, the numeric content ends and I have several consecutive rows all composed of NaN till the end of the matrix. I attach here the code I am using:
v_cutin=2.5;
v_cutout=15;
v_rated=5.5;
v=linspace(v_cutin,v_cutout,15);
D=2.5;
R=D/2;
r=linspace(0.15*R,R*0.97,100);
omega_rpm=linspace(1,600,200);
dataset_red2_lin=NaN*ones(length(omega_rpm)*length(v)*length(r)+length(omega_rpm)*length(v)-1,7);
row_2=0;
i_eff=0;
for i=1:length(dataset2_lin)
row_2=row_2+1;
if row_2<=100
i_eff=i_eff+1;
dataset_red2_lin(i,:)=dataset2_lin(i_eff,:);
elseif row_2>100
dataset_red2_lin(row_2,:)=NaN*ones(1,7);
row_2=0;
end
end
save ('Folder path','dataset_red2','-ascii','-tabs');
In the attachment tyou can find the initial matrix dataset2 in the folder named "DATA" and the resultant txt file in the folder named "RESULT" .
Would the receiving program just happen to also be able to read xls or xlsm files? Getting blanks into those is easier (numeric nan are converted to emptiness in those.)
fmt = "%.7g"; %important that it is string not character vector
writematrix(standardizeMissing(compose(fmt, A), "NaN"), 'dataset_red2.txt', 'delimiter', 'tab')
Adjust the fmt as needed.
In the case where it would be acceptable to use 5 digits of precision, then you can use
writematrix(string(A), 'dataset_red2.txt', 'delimiter', 'tab')
Unfortunately the receiving program is Tecplot. And the user of the receiving program asked me for a txt file with the afore mentioned features. As soon as I work on this, I will try your code and let you know if it works. Thank you man!
Emilio Pulli
Emilio Pulli on 2 Dec 2021
Edited: Emilio Pulli on 2 Dec 2021
Ok perfect, it works! Really really thank you!
The last doubt: do you know how to replace a number that has a lower number of digits with respect to the other numbers in the matrix with the same number with zero digits in order to have the same format of the other numbers? For instance, if I have a “1” in a matrix where all the other numbers are decimal with 4 decimal digits, how can I transform it into “1.0000”?
Use the version with compose and fmt

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!