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
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
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
why do you have this
if true
statements in your code? You should not have function callbacks within an if-condition
@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
Sandy Baha
on 25 Dec 2015
Edited: Sandy Baha
on 29 Dec 2015
its working thank You Very Much for your help
I have big excel file I want to plot specific data using checkbox the above code as suggested is importing data form excel and also plotting the graphs, but I want to plot the graphs by checkbox buttons in next callback. Pls help
Sandy Baha
on 29 Dec 2015
Edited: Image Analyst
on 29 Dec 2015
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)
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
a = numbers(:, 1); % For example, x is in column 1
b = numbers(:, 2);
c = numbers(:, 3);
d = numbers(:, 4);
g = numbers(:, 5);
h = numbers(:, 6);% For example y is in column2
% Plot into the current axes. It will create an axes control if none exists yet.
figure();
plot(a, b, 'b-', 'LineWidth', 2);
grid on;
%%text(a,b,strValues,'VerticalAlignment','bottom');
figure();
plot(a, c, 'r-', 'LineWidth', 2);
grid on;
figure();
plot(a, d, 'y-', 'LineWidth', 2);
grid on;
figure();
plot(a, g, 'g-', 'LineWidth', 2);
grid on;
figure();
plot(a, h, 'v-', 'LineWidth', 2);
grid on;
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject handle to checkbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
CheckBoxChecked(handles);
function CheckBoxChecked(handles)
end
OK, what is this? Your final code? Do you have any more questions?
Sandy Baha
on 29 Dec 2015
Edited: Sandy Baha
on 29 Dec 2015
Yes Image analyst, i want to plot graph using checkbox, by above code i am able to import excel data and plot by pushbutton but my excel file has more data and i wish to plot only desired one by using checkbox. Can you please help fr the same.
Have checkboxes for cases a - h and replace your plotting code with this code, which checks if each column should be plotted:
if get(handles.chkb, 'Value')
plot(a, b, 'b-', 'LineWidth', 2);
grid on;
title('a vs. b', 'FontSize', 20);
end
if get(handles.chkc, 'Value')
plot(a, c, 'r-', 'LineWidth', 2);
grid on;
title('a vs. c', 'FontSize', 20);
end
if get(handles.chkd, 'Value')
plot(a, d, 'y-', 'LineWidth', 2);
grid on;
title('a vs. d', 'FontSize', 20);
end
if get(handles.chkg, 'Value')
plot(a, g, 'g-', 'LineWidth', 2);
grid on;
title('a vs. g', 'FontSize', 20);
end
if get(handles.chkh, 'Value')
plot(a, h, 'v-', 'LineWidth', 2);
grid on;
title('a vs. h', 'FontSize', 20);
end
Hi Image analyst thanks for assistance but it shows some error listed below...i am sharing the snap of my gui

Error in ==> @(hObject,eventdata)imprt('checkbox1_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
I think you left off a critical part of the error message. Please copy and paste ALL the red text so I can see what the error actually was. And let me know if the error was thrown by the button callback, or in one of the checkbox callbacks.
Yes Image analyst,,,,its showing below error
Undefined function or variable 'a'
Error in imprt_new_rev001>checkbox1_Callback (line 133) plot(a, b, 'b-', 'LineWidth', 2);
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in imprt_new_rev001 (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)imprt_new_rev001('checkbox1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Okay, so what is "a"? Remember, you were supposed to assign it with
a = numbers(:, 1); % For example, x is in column 1
You had that in your original code. It appears that you did not do that this time. Why not?
I think you would really really benefit from watching this: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
Sure Image analyst i will go through this video. Thanks for code given by u for checkbox. Its working but my requirement is first excel data should be import and when i tick checkbox it should plot the graph... now with above code i need to select checkbox first and then i need to import the data for plotting.
Sandy, you can have both the button callback and the checkbox callbacks call the very same function, maybe called ImportAndPlotData(). That function can
- call xlsread() and
- check the status of the checkboxes and
- plot the appropriate data.
function ImportAndPlotData()
% call xlsread()
data = xlsread(.................
% check the status of the checkboxes and
% plot the appropriate data.
if get(.................
plot(a,................
Then in the callbacks of all the checkboxes and button, have this
ImportAndPlotData();
You could check the checkbox statuses first and if none are checked you could just exit, or import the data if you want to make it global for other functions to do something with it.
Thanks Image analyst.
I am doing some other work where i need to import multiple excel files and plotting the data in one graph. the code written gives two graphs but individually. I want to plot that in one figure. Please assist me for the same.
if true
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)
[fileList, folder] = uigetfile('*.xlsx',...
'Find the File to Import', ...
'Multiselect', 'on')
for k = 1:length(fileList);
baseFileName = fileList{k};
fullFileName = fullfile(folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Process this file with your code
% (I didn't check this part)
numbers = xlsread(fullFileName); % read the xlsx file
a = numbers(:,1);
b = numbers(:,2);
c = numbers(:,3);
d = numbers(:,4);
e = numbers(:,5);
figure()
hold on
plot( a,b,'g');
hold on
plot( a,b,'g');
hold off
end
end
Take out the call to figure().
Sorry i am very new to Matlab, may you please explain more.
Note in the code below, it's your code but I removed the call to figure() so that it no longer brings up a brand new figure at each iteration of the loop.
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)
[fileList, folder] = uigetfile('*.xlsx',...
'Find the File to Import', ...
'Multiselect', 'on')
for k = 1:length(fileList);
baseFileName = fileList{k};
fullFileName = fullfile(folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Process this file with your code
% (I didn't check this part)
numbers = xlsread(fullFileName); % read the xlsx file
a = numbers(:,1);
b = numbers(:,2);
c = numbers(:,3);
d = numbers(:,4);
e = numbers(:,5);
hold on
plot( a,b,'g');
plot( a,b,'g');
hold off
end
end
Also, I took out the superfluous "hold on" because once you set it on, you don't need to do it again.
And why did you ignore my recommendation to have a single ImportAndPlotData() function and put that in the callbacks for every checkbox and the push button so that you don't have to replicate this code in every one of those callbacks?
Sandy Baha
on 10 Feb 2016
Edited: Sandy Baha
on 10 Feb 2016
Hello, I want to import single as well as multiple files, by above code i am able to import multiple files but if i select single files it shows error, please help me to get the result. I have done something like
if true
[fileList, folder] = uigetfile('*.txt',...
'Find the File to Import', ...
'Multiselect', 'on')
for k = 1:length(fileList)
if k==1
baseFileName = fileList
fullFileName = fullfile(folder, baseFileName);
fprintf(1, 'Now reading %s\n',fullFileName );
raw_data = importdata(fullFileName)
s = raw_data.data
else
baseFileName = fileList{k}
fullFileName = fullfile(folder, baseFileName);
fprintf(1, 'Now reading %s\n',fullFileName );
raw_data = importdata(fullFileName)
s = raw_data.data
end
var = s(:,3)
s(:,3)= s(:,1)
s(:,1)= var
s = sortrows(s)
end
You unfortunately forgot to post the error. Paste all the red text here.
Sandy Baha
on 10 Feb 2016
Edited: Sandy Baha
on 10 Feb 2016
it show: Error using fprintf Function is not defined for 'cell' inputs. Error in Ex_1 (line 12) fprintf(1, 'Now reading %s\n', fullFileName)
fileList may still be a cell. You can get the string from it by doing this
baseFileName = fileList{k};
If that works, then just pull it out of both the if and the else and put it before. Another way is to cast it to char:
baseFileName = char(fileList(k));
or I think you could even use cell2mat() instead of char. Before the fprintf(), make sure you look at the type of fullFileName and make sure it's not a cell array anymore - it should be a char data type, which is the data type of a string variable.
Please read the FAQ to get a good intuitive feel for how cell arrays work: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
Thanks Image Analyst...
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
More Answers (1)
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 Programming in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)