How to call array with string

8 次查看(过去 30 天)
Hello,
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
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
end
% 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};
plot(app.UIAxes,x,y,'-r');
end
end
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.

采纳的回答

Stephen23
Stephen23 2020-11-24
编辑:Stephen23 2020-11-24
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';
datenbank.(str)
(obviously you will need to add the nesting for the app, etc).
  1 个评论
Edward Schreiner
Edward Schreiner 2020-11-24
Thank you I understand now. I got it working with the help of your answer.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by