How to plot multiple variables with independent Y axis (>2) on a UIAxis (App Designer)?

63 Views Asked by At

I am trying to plot multiple variables onto a UIAxes, I currently normalise the data before plotting due to the large variance in variable values, then plot them all against the single standard Y Axis.

I want to have independent Y Axis values / labels for each plotted variable. I have tried to utilise some of the community developed functions, such as addaxis, MyAxisC, PlotNy etc - However they all seem to be designed for use with standard Matlab Figures, as oppose to the UIAxes found in the App Designer.

Can anyone offer any assistance or direction as to how to go about this? I understand the problems with plotting too many variables on a single chart, however my requirements make this necessary.

I currently plot my data like this;

selectedVars = app.SelectedVariablesListbox.Items;
selectedFiles = app.SelectedFilesListBox.Items;
selectedFileIndex = find(contains(app.FileNames, selectedFiles));

if isempty(selectedFiles) || isempty(selectedVars)
    cla(app.UIAxes);
    return;
end

cla(app.UIAxes);
hold(app.UIAxes, 'on');
app.plottedLines = {};
maxSize = 0;
tableData = cell(0, 0);
columnNames = {};
app.UIAxes.YTick = [];
app.UIAxes.YTickLabel = {};
colorMap = lines(max(length(selectedFiles), length(selectedVars)));

for fileIdx = 1:length(selectedFileIndex)
    currentData = app.allLoadedData{selectedFileIndex(fileIdx)};
    for varIdx = 1:length(selectedVars)
        varName = selectedVars{varIdx};

        if length(selectedFileIndex) == 1
            colorToUse = colorMap(varIdx, :);
        else
            colorToUse = colorMap(fileIdx, :); 
        end
    
        if ~isfield(currentData, varName)
            continue;
        end
    
        data = currentData.(varName).data;
        time = time2num(currentData.(varName).time, 'seconds');
        maxSize = max(maxSize, length(data));
        normalizedData = (data - min(data)) / (max(data) - min(data));
    
        if length(selectedFileIndex) == 1
            app.p = plot(app.UIAxes, time, normalizedData, 'DisplayName', varName, 'Color',colorToUse);
        else
            app.p = plot(app.UIAxes, time, normalizedData, 'DisplayName', [app.FileNames{selectedFileIndex(fileIdx)}, ': ', varName], 'Color', colorToUse); 
        end

        app.plottedLines{end+1} = app.p;
        app.UIAxes.TickLabelInterpreter = "none";
        app.p.UserData = data;
        timeRow = dataTipTextRow('Time:', time);
        dataRow = dataTipTextRow(append(varName, ': '), app.p.UserData);
        app.p.DataTipTemplate.DataTipRows = [timeRow, dataRow];
    end
end

legend(app.UIAxes, 'show', 'Interpreter', 'none', 'textcolor', 'white');
hold(app.UIAxes, 'off');
0

There are 0 best solutions below