How to use multiple button function by using 1 excel data?

4 views (last 30 days)
Hi, I want to develope the tool using Matlab GUI in which by using multiple Pushbutton function by using 1 data. The 1st push button should import excel file, the second one plot the graph based on the import excel and last one shows bar graph based on calculation. I didnt get how to link from button plot & bar with the import excel file.The attachment shows the coding & the excel file.

Accepted Answer

Johannes Hougaard
Johannes Hougaard on 27 Apr 2022
I think you are pretty close from what I can see.
The properties of the app are kept as variables and can be obtained from any callback or function.
I would therefore wait a few lines to set the app.UITable.Data value until you have updated your 'data' variable in the UploadButtonPushed
function UploadButtonPushed(app, event)
[file,path,~]=uigetfile;
app.filename=[path file];
data=readtable(file);
% app.UITable.Data=data;
app.UITable.ColumnName = data.Properties.VariableNames;
data.Properties.VariableNames{1}='Load mass';
data.Properties.VariableNames{2}='RBB';
data.Properties.VariableNames{3}='LBB';
data.Properties.VariableNames{4}='RES';
data.Properties.VariableNames{5}='LES';
app.UITable.Data=data; % Save data in app after setting variable names
% Not used in this function - could be removed from here to 'end'
x = table2array(data(:,"Load mass"));
y = table2array(data(:,"RBB"));
y2=table2array(data(:,"LBB"));
y3=table2array(data(:,"RES"));
y4=table2array(data(:,"LES"));
R = rmmissing(x);
R1 = rmmissing(y);
R2 = rmmissing(y2);
R3 = rmmissing(y3);
R4 = rmmissing(y4);
end
And then I'd refrain from loading data again in the other callbacks by reusing the data from the table
function PlotButtonPushed(app, event)
data=app.UITable.Data;
x = table2array(data(:,"Load mass"));
y = table2array(data(:,"RBB"));
y2=table2array(data(:,"LBB"));
y3=table2array(data(:,"RES"));
y4=table2array(data(:,"LES"));
plot(app.UIAxes,x,y,x,y2,x,y3,x,y4);
title(app.UIAxes, 'Load Mass vs ALL')
xlabel(app.UIAxes, 'Load Mass')
ylabel(app.UIAxes, 'All')
legend(app.UIAxes,'RBB','LBB','RES','LES');
end
And similarly the MPButtonPushed as (unless you define x, y, R etc. as public properties and access them by app.x rather than x, which would be slightly more efficient)
function MPButtonPushed(app, event)
data=app.UITable.Data;
x = table2array(data(:,"Load mass"));
y = table2array(data(:,"RBB"));
y2=table2array(data(:,"LBB"));
y3=table2array(data(:,"RES"));
y4=table2array(data(:,"LES"));
R = rmmissing(x);
R1 = rmmissing(y);
R2 = rmmissing(y2);
R3 = rmmissing(y3);
R4 = rmmissing(y4);
themean=mean(R);
themean1= mean(R1);
themean2= mean(R2);
themean3= mean(R3);
themean4= mean(R4);
m = sum((x - themean).*(y- themean1));
p = sum((x - themean).*(x - themean));
l1 = m/p;
%%LBB MP
m2 = sum((x - themean).*(y2 - themean2));
l2 = m2/p;
%%RES MP
m3 = sum((x - themean).*(y3 - themean3));
l3 = m3/p;
%%LES MP
m4 = sum((x - themean).*(y4 - themean4));
l4 = m4/p;
c = categorical({'RBB','LBB','RES','LES'});
title(app.UIAxes, 'MP')
xlabel(app.UIAxes, 'Type')
ylabel(app.UIAxes, 'Cycle')
bar(app.UIAxes, c, [l1;l2;l3;l4],'LineWidth',0.4);
end
You could make several minor optimisations e.g. by adding a filter to your uigetfile and by adding a StartUpFcn to disable the Plot and MP buttons until you have loaded data, but that's just bells and whistles - I think the above code will implement your minimum requirements

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!

Translated by