Access Variable in MATLAB Structure Using User Input

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 4K views
peterjaybee
Messages
62
Reaction score
0
Hi I am trying to use MATLAB for some data analysis. I start by getting user input for a variable called exptno, Then I want to access a location in a structure, but to do this I need to append the variable exptno to the name of the structure. Ill try and give an example to hopefully make it cleared.

I have structure called experiments and substructures called Expt_1, Expt_2, Expt_3, Expt_4 etc... and I want to get a value from in the Expt_# substructure (called spectralfrequencyTD2) based on the value that the user has input to exptno.

The original method I tried was as follows using the int2str command. This doesn't work, but hopefully it will give you some idea of what I m trying to do.

LarmorFreq = experiments.Expt_int2str(exptno).SpectralFrequencyTD2

Thanks,

Peter
 
Physics news on Phys.org
try creating a string using string_const = sprintf('Expt_%i', num);

LarmorFreq = experiments.string_const.SpectralFrequency TD2