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

3 views (last 30 days)
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
Jan
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?

Sign in to comment.

Accepted Answer

Image Analyst
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
Sandy Baha
Sandy Baha on 10 Feb 2016
I have one other concern related to giving text to curve on plot,,,,,Sir, i have some text files which i will be importing and plotting the data. Data is in below format(shown in table below), i want to have plot between x and y keeping unique value same i.e.z data, it should display that unique value on graph for corresponding curve, means i want to plot between x axis = 100 200 300 and y =0.1 0.2 0.3 but there z value is 1 which will be as a text on plot. I have done something but not getting values for every curve, please help
if true
x y z a
100 0.1 1 65
100 0.4 2 71
100 0.7 3 66
200 0.2 1 55
200 0.5 2 68
200 0.9 3 70
300 0.3 1 72
300 0.6 2 77
300 1.0 3 67
end
if true
[fileList, folder] = uigetfile('*.txt',...
'Find the File to Import', ...
'Multiselect', 'on');
fileList = cellstr(fileList);
for k = 1:length(fileList)
baseFileName = fileList{k}
fullFileName = fullfile(folder, baseFileName)
fprintf(1, 'Now reading %s\n', fullFileName)
raw_data = importdata(fullFileName)
s = raw_data.data
var = s(:,3)
s(:,3)= s(:,1)
s(:,1)= var
s = sortrows(s)
E=unique(s(:,1))
pos=[];
for i=1:length(E)
k=find(s==E(i));
k=max(k)+i;
pos=[pos;k]
end
[r,c] = size(s);
add = numel(pos); % How much longer Anew is
Anew = NaN(r + add,c); % Preallocate
idx = setdiff(1:r+add,pos); % all positions of Anew except pos
Anew(idx,:) = s;
fclose('all')
x=Anew(:,1);
y=Anew(:,2);
z=Anew(:,3);
a=Anew(:,4);
hold on
figure(1)
plot(z,y,'*-','DisplayName',baseFileName);
hold all
for j = 1:length(E);
text(z(j),y(j),num2str(E(j)))
end
legend('-DynamicLegend','Location','southwest');
xlabel('x');
ylabel('y');
end

Sign in to comment.

More Answers (1)

Jan
Jan on 24 Dec 2015
Edited: Jan on 24 Dec 2015
I'm not sure, what your question is. But I guess, this could be useful:
function disp_excel_Callback(hObject, eventdata, handles)
handles = guidata(hObject); % Get the current version of handles
dataset = handles.fileData;
...

Categories

Find more on 2-D and 3-D Plots 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!