how to upload a random excel file and read it

4 次查看(过去 30 天)
I am working on an assignment in which i want to develope a stand alone app (GUI) of curve fitting.
in which i need to get data input from a random excel file,and read that file to make a graph.
under 'upload file'i wrote this fuction
function pushbutton2_Callback(hObject, eventdata, handles)
a= uigetfile({'*.xlsx'},'select the excel file');
b=xlsread('a);
under 'calculate' i wrote this fuction
function pushbutton1_Callback(hObject, eventdata, handles)
x=dataset(:,1);
y=dataset(:,2);
p=polyfit(x,y,5);
xp=0:0.1:0.5;
yp=polyval(p,xp);
plot(x,y,'--',xp,yp);
grid;
-- its not working,i want matlab to open a 'select window' so a user can upload a rondom excel file(which contain data),and when the user click the 'calculate button' it will fit the curve.

回答(1 个)

Sylvain
Sylvain 2020-2-20
The trick is that your functions have local variables, you need to send them out using the handles.
you may use the import data toolbox , and generate an import function.
before generating a function, template the out put
generate the code importfile.m and modify it so that your function is
function out = importfile(filename)
...
end
then use this function in your upload file button call back:
function pushbutton2_Callback(hObject, eventdata, handles)
a= uigetfile({'*.xlsx'},'select the excel file');
b = importfile(filename);
handles.dataset = b;
guidata(hObject, handles);
end
now use the handles to retrieve the data and plot the results
function pushbutton1_Callback(hObject, eventdata, handles)
x=handles.dataset(:,1);
y=handles.dataset(:,2);
p=polyfit(x,y,5);
xp=0:0.1:0.5;
yp=polyval(p,xp);
plot(x,y,'--',xp,yp);
grid;
end

产品

Community Treasure Hunt

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

Start Hunting!

Translated by