What is evaluated to standard output is not being assigned to my variable
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Kristoffer Walker
am 13 Mär. 2025
Bearbeitet: Stephen23
am 14 Mär. 2025
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?
2 Kommentare
Akzeptierte Antwort
John D'Errico
am 13 Mär. 2025
Bearbeitet: John D'Errico
am 13 Mär. 2025
This is a not uncommon misunderstanding about cell arrays.
d = {'.';
'..'
'2022'
'2024'
'2025'}
b = d(3:end) % the "right" way
When you used curly braces instead to extract them, did you see what happened?
d{3:end} % A comma separated list
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}
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}
And, along those lines, you COULD have tried this:
[b_kludge{1:3}] = d{3:end}
It seems a bit of a kludge to me.
And, finally, you could have done this
b_kludge2 = {d{3:end}}'
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 Kommentar
Stephen23
am 14 Mär. 2025
Bearbeitet: Stephen23
am 14 Mär. 2025
"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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Whos finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!