How do I access an element in an answer array directly?

조회 수: 4 (최근 30일)
Kishore
Kishore 2025년 4월 24일
댓글: Stephen23 2025년 4월 28일
I have a variable named snsr_type as defined below. I want extract just "Diff" and store it in snsr_type variable. I have to do this in 2 steps.
snsr_name = "Diff sensor A1";
snsr_type = split(snsr_name,' ');
snsr_type = snsr_type(1);
If I combine those last 2 lines like this, it errors out saying "Error: Invalid array indexing.". I've also tried wrapping the first part in () like (split(snsr_name,' '))(1). This also doesn't work.
snsr_type = split(snsr_name,' ')(1);
Is there any way of doing this simply in a single line? Or do I have to do this in 2 lines?
  댓글 수: 6
Image Analyst
Image Analyst 2025년 4월 26일
snsr_name = "Diff sensor A1";
snsr_type = extractBefore(snsr_name,' ') % Get first word, which is the sensor type
snsr_type = "Diff"
Kishore
Kishore 2025년 4월 28일
Thanks @Paul Lambrechts and @Image Analyst. I think the extractbefore function will work in this very specific case. This question was more about accessing specific elements of an array directly in the same line as the (answer) array is being created. I do not think the extractbefore function is capable of that (for ex., what if my answer array is numeric).
Therefore, I think the solution for my question is still that it needs to be done in 2 steps as @Image Analyst answered earlier. Thanks for your help!

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

채택된 답변

Image Analyst
Image Analyst 2025년 4월 24일
Evidently you need to do it in two steps. A bonus though is that the two step method is much more readable and understandable. I think the single line way would be more confusing - a head scratcher for most people.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2025년 4월 28일
Two approaches:
First = @(V) V(1);
snsr_name = "Diff sensor A1";
snsr_type1 = First(split(snsr_name,' '))
snsr_type1 = "Diff"
snsr_type2 = struct('Data', split(snsr_name,' ')).Data(1)
snsr_type2 = "Diff"
  댓글 수: 2
Steven Lord
Steven Lord 2025년 4월 28일
A third (somewhat complicated) way:
snsr_name = "Diff sensor A1";
y = subsref(split(snsr_name, ' '), substruct('()', {1}))
y = "Diff"
Stephen23
Stephen23 2025년 4월 28일
The simplest approach is likely to be faster in many situations:
timeit(@f0)
ans = 6.7571e-07
timeit(@f1)
ans = 2.3588e-06
timeit(@f2)
ans = 4.9818e-06
timeit(@f3)
ans = 8.9755e-06
function f0()
snsr_name = "Diff sensor A1";
snsr_type = split(snsr_name,' ');
snsr_type = snsr_type(1);
end
function f1()
First = @(V) V(1);
snsr_name = "Diff sensor A1";
snsr_type = First(split(snsr_name,' '));
end
function f2()
snsr_name = "Diff sensor A1";
snsr_type = struct('Data', split(snsr_name,' ')).Data(1);
end
function f3()
snsr_name = "Diff sensor A1";
snsr_type = subsref(split(snsr_name, ' '), substruct('()', {1}));
end

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

카테고리

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

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by