Replace empty string with NaN in a cell

147 vues (au cours des 30 derniers jours)
Daneisha Blair
Daneisha Blair le 23 Août 2021
Commenté : Daneisha Blair le 23 Août 2021
I have a cell called A={ "20" "25" "25" [] "20" [] "25" "25" "25" "30"}
I would like to replace cell A with A={ "20" "25" "25" "NaN" "20" "NaN" "25" "25" "25" "30"}
Any help is highly appreciate, thank you.

Réponse acceptée

Stephen23
Stephen23 le 23 Août 2021
Modifié(e) : Stephen23 le 23 Août 2021
A = {"20","25","25",[],"20",[],"25","25","25","30"}
A = 1×10 cell array
{["20"]} {["25"]} {["25"]} {0×0 double} {["20"]} {0×0 double} {["25"]} {["25"]} {["25"]} {["30"]}
A(cellfun(@isempty,A)) = {"NaN"}
A = 1×10 cell array
{["20"]} {["25"]} {["25"]} {["NaN"]} {["20"]} {["NaN"]} {["25"]} {["25"]} {["25"]} {["30"]}
Using one string array is likely much better than nesting lots of scalar strings inside a cell array:
B = string(A)
B = 1×10 string array
"20" "25" "25" "NaN" "20" "NaN" "25" "25" "25" "30"
  1 commentaire
Daneisha Blair
Daneisha Blair le 23 Août 2021
This is perfect! Thank you.

Connectez-vous pour commenter.

Plus de réponses (1)

Awais Saeed
Awais Saeed le 23 Août 2021
Modifié(e) : Awais Saeed le 23 Août 2021
clc;clear;close all;
A={ "20" "25" "25" [] "20" [] "25" "25" "25" "30"}
% get locations of empty cell elements
idx = cellfun('isempty',A);
% replace those empty cells with NaN
A(idx) = {NaN}

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by