Filter löschen
Filter löschen

Easy Question - Set Variable to Another Variable Using sprintf

4 Ansichten (letzte 30 Tage)
I have a GUI with drop-down boxes, and based on these drop-down box values I load a specific variable (large matrix) out of my .mat file.
Here's that code which works:
STstr = get(handles.source_type,'String');
STval = get(handles.source_type,'Value');
switch STstr{STval};
case 'Single Point Source'
source_type='SingleHeightMesh';
end
SHstr = get(handles.scan_height, 'String');
SHval = get(handles.scan_height,'Value');
switch SHstr{SHval};
case '100ft'
scan_height='100';
end
filespec='%s%s';
load('MCNPdata.mat',sprintf(filespec,source_type,scan_height));
The problem is that the rest of my code uses another variable, HeightMesh which I need to assign to the large matrix I just loaded.
HeightMesh=sprintf(filespec,source_type,scan_height);
This code just assigns HeightMesh to the string value of the name of that matrix variable. How can I simply take this matrix I loaded (the name of the matrix will vary based on multiple drop-down options) and assign it to HeightMesh?
Thanks in advance.

Akzeptierte Antwort

Shashank Prasanna
Shashank Prasanna am 31 Jan. 2013
Bearbeitet: Shashank Prasanna am 31 Jan. 2013
S = load('MCNPdata.mat',sprintf(filespec,source_type,scan_height));
S is a structure and one of its elements will be the matrix.
HeightMesh=S.(sprintf(filespec,source_type,scan_height));
Dynamically access the matrix as above
  2 Kommentare
Shashank Prasanna
Shashank Prasanna am 31 Jan. 2013
Here is an example that works for demonstration:
S=load('accidents.mat','datasources');
HeightMesh=S.('datasources');
Ed
Ed am 1 Feb. 2013
Works great - thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by