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에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
