How to call array with string

조회 수: 10 (최근 30일)
Edward Schreiner
Edward Schreiner 2020년 11월 24일
댓글: Edward Schreiner 2020년 11월 24일
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