Return value from a UITABLE

Hi All,
Does anyone know if there is a way to search for a specific value in a UI Table and return the value in the column next to it?
I am using a GUI and the user selects a value from a lsitbox and I would like to look for that selected value in the 3 column UITABLE that is in the GUI (that already has data in it) and return the value in the column next to it.
They value being searched for is always in column 1 of the UITABLE (if that helps at all).
Any help is appreciated!!
Rob

 채택된 답변

Walter Roberson
Walter Roberson 2013년 2월 14일
편집: Walter Roberson 2013년 2월 14일

0 개 추천

curdata = get(handles.uitable, 'data');
tf = ismember(curdata(:,1), ValueToLocate);
adjacent_value = curdata(tf, 2);
if numel(adjacent_value) ~= 1
error('match fail');
end
This would probably leave you with a 1 x 1 cell, so use adjacent_value{1}

댓글 수: 5

mtr
mtr 2013년 2월 14일
Walter,
Thanks for the quick response. Unfortunatley I get the following error:
_Error using cell/ismember>cellismemberlegacy (line 131) Input A of class cell and input B of class double must be cell arrays of strings, unless one is a string.___
So my UI Table contains cells with text and numbers. I am hoping to search for a TEXT cell and returning a number cell.
And I am loading UITABLE using the following:
[num,txt,all] = xlsread(fileName);
h = handles.uitable1;
set(h, 'data', all);
Walter Roberson
Walter Roberson 2013년 2월 14일
The error message you display indicates that the value you are searching for is a double, not a text string ?
mtr
mtr 2013년 2월 14일
Yeah, so it was a double. So I converted it to a tring but this is my issue- My UI Table has 2 columns: Column1: K101 to K105 | Column 2: 1 to 5
My List box gets populated with the first column (K101 to K105) using: set(handles.LoadDataLB, 'String', txt);
Unfortunatley, when I use "ismember" it only returns to me the row number of the list box that is selected and not the actual value. I found this out when I tried to concatenate the letter "K" with the number using the following:
rownumber= num2str(get(handles.LoadDataLB, 'Value'));
lookfor=sprintf('%s', 'K', rownumber);
curdata = get(handles.uitable1, 'data');
tf = ismember(curdata(:,1), lookfor);
adjacent_value = curdata(tf, 2); end
But this tell matlab to look for "K1" which doesnt exist. Thoughts?
Are the uitable entries in a different order? If not, then the entry you want is
curdata{rownumber, 2}
mtr
mtr 2013년 2월 18일
No they are in the same order. Walter, I was actually able to get this working using a response from you on another thread (go figure! http://www.mathworks.com/matlabcentral/answers/24300):
strings = get(hObject, 'string');
curval = get(hObject, 'value');
if iscell(strings)
curstring = strings{curval};
else
curstring = strings(curval, :); %char array
end
Thanks for all the help!
R

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

추가 답변 (1개)

Image Analyst
Image Analyst 2013년 2월 14일

0 개 추천

Get the table data:
tableData = get(handles.table1, 'Data');
You might have to use cell2mat(tableData) - I don't remember. Then use extract the column you're looking for and use ismember() or find() to find the matching value. Be sure to know the FAQ. So you might want to do
theColumn = tableData(:, 1); % Get column 1
row = find(abs(theColumn - theSoughtValue) < 0.00001);
Then just get the next column.
theValueWeWant = table(row, 2);

댓글 수: 1

mtr
mtr 2013년 2월 18일
Thanks Image Analyst, I was able to get it working using walters code above but appreciate the suggestion.
R

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

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

mtr
2013년 2월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by