MATLAB Answers

How to call array with string

2 views (last 30 days)
I'm writing a Matlab App Designer program to help visualize my Data.
Since there are a lot of different settings and therefore a whole lot Data to plot I created an array containing all of my processed Data and it's variations ('Datenbank.mat').
Now I want to be able to set what Data I want to plot on the left, then click the Button 'plot' and get my desired graph.
For this I tried using the user input as indices, so I can use them to acces the needed data from my struct array 'datenbank'. The following code should represent what I'm trying to do:
properties (Access = private)
datenbank = load('Datenbank.mat'); % Description
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
% Button pushed function: plotButton
function plotButtonPushed(app, event)
index1 = app.VersuchsnummerEditField.Value;
index2 = str2num(app.DurchlaufDropDown.Value);
[~,index3] = ismember(app.MessgreDropDown.Value, app.MessgreDropDown.Items);
[~,index4] = ismember(app.TypDropDown.Value, app.TypDropDown.Items);
[~,index5] = ismember(app.AbschnittDropDown.Value, app.AbschnittDropDown.Items);
titel = {'ges','e','m','a','fft_ges','fft_e','fft_m','fft_a'};
index5 = titel(index5);
[~,index6] = ismember(app.FilterungDropDown.Value, app.FilterungDropDown.Items);
% Create Variable name !! My Problem !!
x = {'app.datenbank.datenbank(' index1 ').Plots(1).Index.' index5};
y = {'app.datenbank.datenbank(' index1 ').Plots(' index2 ').Data(' index3 ').' index5};
Since this is not the right syntax it doesn't work, but I know that when I insert a variable name from my data array for x and y I get my desired graph so I just need to implement proper naming of the variable. The problem lies within 'index5' since I need to set it to the right string to call the correct array. I know that naming variables dynamically is not recommended in Matlab, so I'm open to suggestions to solve this whole thing in another way.


Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 24 Nov 2020
Edited: Stephen Cobeldick on 24 Nov 2020
Fieldnames are not indices (or atleast, not in the way that you are trying to use them). And writing the app variable, loaded variable name, etc. in one long string indicates that you are definitely going down the wrong path.
Instead you can simply use dynamic fieldnames to access the loaded data:
For example:
str = 'ges';
(obviously you will need to add the nesting for the app, etc).

  1 Comment

Edward Schreiner
Edward Schreiner on 24 Nov 2020
Thank you I understand now. I got it working with the help of your answer.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!