Picking Variable from Workspace

13 ビュー (過去 30 日間)
Dheeraj Kapur
Dheeraj Kapur 2016 年 3 月 4 日
編集済み: Stephen23 2016 年 3 月 8 日
I have three variables pertaining to the same physical quantity (recorded from three different sources). Now, when I process the data, I want to give the control to user to pick one of the three variables based on which source of data he/she considers more reliable. I would like to open a dialog box and make the user pick one variable, copy that variable in another temporary variable within my script and then perform various operations/analsyes. What would be the best way to do something like this?
  1 件のコメント
Stephen23
Stephen23 2016 年 3 月 4 日
編集済み: Stephen23 2016 年 3 月 8 日
The best way would be to keep the values in one variable and use indexing or dynamic fieldnames.

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

回答 (2 件)

Steven Lord
Steven Lord 2016 年 3 月 4 日
Store your data as fields in a struct array. Use FIELDNAMES to determine the files in the struct and let the user select one of those fields. Use dynamic field names to retrieve the appropriate field.

Dheeraj Kapur
Dheeraj Kapur 2016 年 3 月 8 日
Thank you for your inputs! Actually, I was looking to do something like this (perhaps it wasn't so clear in the original question):
choice = inputdlg(sprintf('Choose the source:\n1: Source 1\n2: Source 2\n3: Source 3'));
if str2double(choice) == 1
source = source1;
elseif str2double(choice) == 2
source = source2;
elseif str2double(choice) == 3
source = source3;
end
where source1, source2 and source3 are three different variables in the workspace. I have managed it with the above lines of code. I'm not sure thought if this is the best way to do this.
Cheers
  1 件のコメント
Stephen23
Stephen23 2016 年 3 月 8 日
編集済み: Stephen23 2016 年 3 月 8 日
"if this is the best way to do this" Probably not. The best way would be to store your data in one variable. Then you can simply use indexing or dynamic field names, or even loop over all of them.

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

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by