What is evaluated to standard output is not being assigned to my variable

조회 수: 5 (최근 30일)
Hello,
I am having a difficult time understanding why what I'm seeing go to standard output is not being assigned to my variable. Could someone explain the rationale or what is going on?
K>> d
d =
5×1 cell array
{'.' }
{'..' }
{'2022'}
{'2024'}
{'2025'}
K>> d{3:end}
ans =
'2022'
ans =
'2024'
ans =
'2025'
K>> b = d{3:end}
b =
'2022'
In the code clip, why does "b" not contain all the standard output shown in the preceding line execution?

채택된 답변

John D'Errico
John D'Errico 2025년 3월 13일
편집: John D'Errico 2025년 3월 13일
This is a not uncommon misunderstanding about cell arrays.
d = {'.';
'..'
'2022'
'2024'
'2025'}
d = 5x1 cell array
{'.' } {'..' } {'2022'} {'2024'} {'2025'}
b = d(3:end) % the "right" way
b = 3x1 cell array
{'2022'} {'2024'} {'2025'}
When you used curly braces instead to extract them, did you see what happened?
d{3:end} % A comma separated list
ans = '2022'
ans = '2024'
ans = '2025'
It created THREE separate objects. See they were listed, not as one single thing, but each got dumped into ans, separately. This is what is called a comma separated list. As well, you should see the result are the three elements of the originalcell array, BUT they are not themselves in the form of cell arrays!
It is an often useful tool, but not here. In fact, this is a very important way to pass in distinct input arguments into a function. You don't want to use a comma separated list though for your problem.
So, when you did this:
bad = d{3:end}
bad = '2022'
And there, we see that only the first element of that comma separated list got dumped into bad. You COULD also have tried this instead, thus creating three distinct variables. (Not what you wanted, again.)
[b1,b2,b3] = d{3:end}
b1 = '2022'
b2 = '2024'
b3 = '2025'
And, along those lines, you COULD have tried this:
[b_kludge{1:3}] = d{3:end}
b_kludge = 1x3 cell array
{'2022'} {'2024'} {'2025'}
It seems a bit of a kludge to me.
And, finally, you could have done this
b_kludge2 = {d{3:end}}'
b_kludge2 = 3x1 cell array
{'2022'} {'2024'} {'2025'}
And that works, because after I created the comma separated list as d{3:end}, I immediately grabbed the entire comma separated list with curly braces! All three objects in the list then got coallesced into a cell array. In my eyes, the last two solutions definitely feel kludgy, ergo the names I assigned them.
Surely you would accept the easy solution was to just use parens instead.
  댓글 수: 1
Stephen23
Stephen23 2025년 3월 14일
편집: Stephen23 2025년 3월 14일
"Surely you would accept the easy solution was to just use parens instead."
I.e. you should have done this:
d(3:end)
But note that if those are data returned by DIR (or similar) then hard-coding the positions of the dot folder names is a bug in your code (they are not guaranteed to be element one and two). The robust approach is to use ISMEMBER or similar to remove the dot directory names. Or even better, by specifying the DIR search string with non-empty name matching.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by