Comparing contents of structures

조회 수: 4 (최근 30일)
Jared
Jared 2013년 1월 23일
A little background info: in a Matlab GUI, I load the files from a directory into a listbox. I have a structure with 8 possible file names and the "display name" that each filename maps to. This display name is what is populated in the listbox.
label{1,1}=file1.csv
label{1,2}=Display 1
label{2,1}=file2.csv
label{2,2}=Display 2
With the following code, I am trying to determine which files are selected, determine the actual file name, and load the files into an originally empty data structure.
data{1,8}={};
for i=1:length(index_selected)
for j=1:8 %8 possible files
if strcmp(handles.list_names{index_selected(i)},labels{j,2})==1
if isempty(data{j})==1
fid=fopen(labels{j,1});
data{j}=textscan(fid,'%s%s%u%f%f%f%f%u','Delimiter',',',...
'HeaderLines',1);
fclose(fid);
end
end
end
end
I know strcmp isn't going to work as I have it, but it illustrated what I'm going for. The main thing I am struggling with is how to get a string of the display name of a selected index.

채택된 답변

Cedric
Cedric 2013년 1월 23일
편집: Cedric 2013년 1월 23일
Not sure that I fully understand (it's late), but note that strcmp() and strcmpi() do work on cell arrays. I illustrate that with a basic example:
NL = {'file1.csv', 'Label 1'; ... % Names and labels
'file2.csv', 'Label 2'; ...
'file3.csv', 'Label 3'} ;
selected = {'Label 1', 'Label 3'} ;
for ii = 1 : numel(selected)
match = strcmp(selected{ii}, NL(:,2)) ;
fprintf( 'Filename for "%s": %s.\n', selected{ii}, NL{match,1}) ;
end
Cheers,
Cedric

추가 답변 (1개)

Image Analyst
Image Analyst 2013년 1월 23일
Have you considered using ismember()?
  댓글 수: 1
Jared
Jared 2013년 1월 23일
I have not. It looks like it might do what I need.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by