Print corresponding value in Cell Array

조회 수: 1 (최근 30일)
PR
PR 2018년 4월 7일
답변: Sumeet Gadagkar 2018년 4월 10일
Hi, I want to write a function which maps a logical array to a cell array: For example, I have this array:
AB 2300 J 1500
BC 992 J 2300
Now I want to request userinput (select a value of the first column). If someone selects "AB", I want to have printed the corresponding value in the row, namely 1500. I struggle to find a good solution,so if anyone can help out, it would be great!
Cheers, PR

답변 (1개)

Sumeet Gadagkar
Sumeet Gadagkar 2018년 4월 10일
Hello,
I hope the following code snippet helps you,
A = {'AB' '2300' 'J' '1500'
'BC' '992' 'J' '2300'};
in = input('Enter Input\n','s');
out = A(find(strcmp(A,in)),end);
The "strcmp" function is used to find the location of the string matching the input and then logical indexing is performed to get the desired output.

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by