Need help to search a structure with user I/P

1 回表示 (過去 30 日間)
R J
R J 2014 年 9 月 2 日
コメント済み: R J 2014 年 9 月 11 日
Hi,
I have a data structure (tempExp) that looks like the following:
tempExp.size --> This is a string
tempExp.color --> This is a string
tempExp.data --> Usually a 1x2000 double
Right now I ask the user make "size" and "color" selections in a gui popupmenu which I save as string variables called "size" and "color". What I would like to do is use that information to select the appropriate tempExp.data field and eventually use that to make plots, or further processing, etc.
For example, user selects "orange" and "medium". How do I use that information to search the tempExp structure that contains the color "orange" and size "medium" and then access/return the corresponding tempExp.data for those user selected fields?
Thank you for your time!
  2 件のコメント
Image Analyst
Image Analyst 2014 年 9 月 2 日
DO NOT SAVE IN A VARIABLE CALLED size!!! You can have a structure field called size, but not a whole string variable called "size" or you will blow away the built-in size() function.
R J
R J 2014 年 9 月 2 日
Duly noted!

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2014 年 9 月 2 日
Assuming that tempExp is an array of struct
sizes = {tempExp(:).size};
colors = {tempExp(:).color};
matchstruct = tempExp(strcmp(sizes, size) & strcmp(colors, color));
if ~isempty(matchstruct)
data = matchstruct.data; %assume that there is at most one match.
%do something with data
end
  4 件のコメント
Guillaume
Guillaume 2014 年 9 月 11 日
If there is more than one match, then matchstruct will be an array of the structures that match. You can then do whatever you want with this array such as:
for match = 1 : numel(matchstruct)
data = matchstruct(match).data;
...
end
R J
R J 2014 年 9 月 11 日
Ah, ok I see. This was very helpful. Thank you again!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by