Hello, I want to develope the tool using Matlab GUI in which by using Pushbutton it should import excel file and by onother pushbutton it should plot the graph. please help me to get the answer
1 view (last 30 days)
Show older comments
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename pathname] = uigetfile({'*.xlsx'},'Open Directory');
%fullpathname = strcat(pathname, filename);
%text = fileread(fullpathname);
if isequal(filename,0) || isequal(pathname,0)
return
end
fileID = fopen(fullfile(pathname, filename));
handles.fileData = fscanf(fileID,'%d');
temp = fscanf(fileID,'%d',[8 Inf]);
handles.fileData = temp';
guidata(hObject, handles);
if true
% function disp_excel_Callback(hObject, eventdata, handles)
% hObject handle to disp_excel (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.fileData
dataset = handles.fileData;
x = dataset(:,1);
y = dataset(:,2);
figure();
plot(x,y,'*');
code
end
2 Comments
Ingrid
on 24 Dec 2015
why do you have this
if true
statements in your code? You should not have function callbacks within an if-condition
Jan
on 24 Dec 2015
Edited: Jan
on 24 Dec 2015
@Ingrid: "if true" appears when you click on the "{} Code" button without selecting code in the forum's interface before. So simply ignore this. As an alternative Sandy could edit the code and remove this confusing pieces...
@Sandy: What is your question? Are you sure that fscanf is the approriate method to import your XLSX files? What about xlsread?
Accepted Answer
Image Analyst
on 24 Dec 2015
Sandy:
Why do it in two steps? That would just annoy the users. As soon as your user specifies the file name, it should plot it right then. There is no need to click a second button, is there? Users don't want to be forced to do extra unnecessary steps
So in the pushbutton callback where they'll specify the file name of your Excel workbook, just have this:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB'; % Whatever you want. Could be pwd if you want.
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.xls*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
[numbers, strings, raw] = xlsread(fullFileName);
% Extract x and y
x = numbers(:, 1); % For example, x is in column 1
y = numbers(:, 2); % For example y is in column2
% Plot into the current axes. It will create an axes control if none exists yet.
plot(x, y, 'b-', 'LineWidth', 2);
grid on;
22 Comments
See Also
Categories
Find more on Graphics Object Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!