How to assign a double value to a cell array?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
My goal is to replace any missing values in my cell array T1 with -999, if the column is composed of floating numbers, and replace any missing values with an empty string like ' ', if the column is composed of text strings?
Below is my code. Unfortunately, I got an error saying: "Conversion to cell from double is not possible."
% Replace the remaining missing values with -999:
missingIndices2 = ismissing(string(T1));
T1(missingIndices2) = -999;
% convert the cell into a table:
Table = cell2table(T1);
採用された回答
Put curly braces {} around the -999, which makes the right-hand side of the assignment a scalar cell array:
T1(missingIndices2) = {-999};
8 件のコメント
Leon
2025 年 3 月 10 日
Thanks for the suggestion. I just tried it. Unfortunately, my table column turns into something like the below:
{[ -999]}
{[ -999]}
{[ -999]}
{[35.9963]}
{[35.8279]}
When it should be
-999
-999
-999
35.9963
35.8279
Voss
2025 年 3 月 10 日
Please upload a mat file with the cell array T1.
"Unfortunately, my table column turns into something like the below: "
It works here:
C = {-999; -999; -999; 35.9963; 35.8279};
T = cell2table(C)
T = 5x1 table
C
______
-999
-999
-999
35.996
35.828
Leon
2025 年 3 月 10 日
Please see attacned for a mat file containing the cell array T1. Many thanks for your help.
C = load('test.mat').T1
C = 1498x2 cell array
{[35.7030]} {[35.7010]}
{[35.5790]} {[35.5780]}
{[35.5790]} {[35.5790]}
{[34.7100]} {[34.7090]}
{[34.7180]} {[34.7190]}
{[34.7170]} {[34.7130]}
{[34.9420]} {[34.9410]}
{[34.3970]} {[34.3820]}
{[34.3970]} {[34.3820]}
{[34.2390]} {[34.2390]}
{[34.2390]} {[34.2390]}
{[34.0750]} {[34.0710]}
{[34.0760]} {[34.0670]}
{[33.4280]} {[33.4240]}
{[33.4280]} {[33.4230]}
{[36.0710]} {[36.0740]}
C(~cellfun(@isscalar,C)) = {-999};
T = cell2table(C)
T = 1498x2 table
C1 C2
______ ______
35.703 35.701
35.579 35.578
35.579 35.579
34.71 34.709
34.718 34.719
34.717 34.713
34.942 34.941
34.397 34.382
34.397 34.382
34.239 34.239
34.239 34.239
34.075 34.071
34.076 34.067
33.428 33.424
33.428 33.423
36.071 36.074
This is probably better handled when the data is imported.
Many thanks.
There is one thing though. The code uses -999, why are the output NaNs instead? Additionally, the T1 I shared was a simplified one. In reality I would have many columns that are text strings as well. Is there a way I could replace missing values in float data columns with -999, but replace missing values in string columns with an empty string like ' '? Thanks.
C(~cellfun(@isscalar,C)) = {-999};
"why are the output NaNs"
Because your cell array has some cells that contain scalar numerics, some cells that contain <missing>s, and some cells that contain (empty) character vectors.
If you use ismissing(string(C)) as the condition to replace with {-999}, then you replace the <missing>s but not the empty character vectors because string('') is "" and "" is not <missing>.
ismissing(string(''))
ans = logical
0
If you use ~cellfun(@isscalar,C) as the condition, then you replace the empty character vectors but not the <missing>s because <missing> is a scalar
isscalar(missing)
ans = logical
1
To replace the contents of any cell that contains <missing> or a non-scalar array, which seems like what you are trying to do [EDIT: not really, thanks for clarifying], you can use some condition like cellfun(@(x)~isscalar(x) || ismissing(x),C)
C = load('test.mat').T1;
rows = [99:108, 576:585];
C(rows,:)
ans = 20x2 cell array
{[33.8186]} {[<missing>]}
{[33.8898]} {[ 33.8903]}
{[33.9370]} {[ 33.9366]}
{[33.9753]} {[ 33.9761]}
{[33.9749]} {[<missing>]}
{[33.9249]} {[ 33.9261]}
{[33.9613]} {[ 33.9631]}
{[34.1909]} {[ 34.1920]}
{[34.2087]} {[<missing>]}
{[34.2238]} {[<missing>]}
{[36.3150]} {[ 36.3090]}
{[36.3190]} {[ 36.3130]}
{[36.3410]} {[ 36.3340]}
{[36.2880]} {[ 36.2920]}
{[36.1500]} {0x0 char }
{[36.6150]} {[ 36.5980]}
C(cellfun(@(x)~isscalar(x) || ismissing(x),C)) = {-999};
T = cell2table(C);
C(rows,:)
ans = 20x2 cell array
{[33.8186]} {[ -999]}
{[33.8898]} {[33.8903]}
{[33.9370]} {[33.9366]}
{[33.9753]} {[33.9761]}
{[33.9749]} {[ -999]}
{[33.9249]} {[33.9261]}
{[33.9613]} {[33.9631]}
{[34.1909]} {[34.1920]}
{[34.2087]} {[ -999]}
{[34.2238]} {[ -999]}
{[36.3150]} {[36.3090]}
{[36.3190]} {[36.3130]}
{[36.3410]} {[36.3340]}
{[36.2880]} {[36.2920]}
{[36.1500]} {[ -999]}
{[36.6150]} {[36.5980]}
T(rows,:)
ans = 20x2 table
C1 C2
______ ______
33.819 -999
33.89 33.89
33.937 33.937
33.975 33.976
33.975 -999
33.925 33.926
33.961 33.963
34.191 34.192
34.209 -999
34.224 -999
36.315 36.309
36.319 36.313
36.341 36.334
36.288 36.292
36.15 -999
36.615 36.598
Leon
2025 年 3 月 10 日
Working now. Thank you so much for the tremendous help!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Structures についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
