Access Variable in MATLAB Structure Using User Input

In summary, To use MATLAB for data analysis, you need to get user input for a variable called exptno and append it to the name of a structure called experiments. This can be done by creating a string using the sprintf command and accessing the desired value through the substructure Expt_# using the variable exptno.
  • #1
peterjaybee
62
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
  • #2
try creating a string using string_const = sprintf('Expt_%i', num);

LarmorFreq = experiments.string_const.SpectralFrequency TD2
 

What is a MATLAB structure?

A MATLAB structure is a data type that allows you to store and organize different types of data, such as numbers, strings, and arrays, in a single variable.

How do I create a structure in MATLAB?

To create a structure in MATLAB, you can use the "struct" function. For example, you can create a structure with three fields named "name", "age", and "gender" by typing: myStructure = struct('name', 'John', 'age', 25, 'gender', 'male').

How can I access a specific field in a MATLAB structure?

You can access a specific field in a MATLAB structure using dot notation. For example, if you have a structure named "myStructure" with a field named "name", you can access it by typing: myStructure.name.

Can I access a variable in a MATLAB structure using user input?

Yes, you can access a variable in a MATLAB structure using user input. You can use the input() function to get user input and then use that input as the field name to access the variable in the structure.

What happens if I try to access a non-existing field in a MATLAB structure?

If you try to access a non-existing field in a MATLAB structure, you will get an error. To avoid this, you can use the isfield() function to check if the field exists before trying to access it.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
689
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Back
Top